MySQL + PHP - 查询相对格式日期
MySQL + PHP - Querying relative format dates
我有两个表:steps 和 flows。
- 步骤有很多流程。
- 步骤有
relative_time
(字符串)列。
- 流有
active_on
(可为空,时间戳)列。
步骤 relative_time
存储 relative formats,它们的值类似于:“+1 年”、“+2 天”、“+30 天”等
流程 active_on
当流程被用户激活时设置,它看起来像:“2017-12-30 00:00:00”。
每次我想知道流什么时候过期,我从数据库中select它,然后在PHP(Laravel ,Carbon), 我执行:
/**
* @return null|Carbon
*/
public function getExpiresAtAttribute()
{
if (!$this->active_on) {
return null;
}
// Access the relative time for step (for example: "+3 days").
$relativeTime = $this->step->relative_time;
// Adds the limit time to the activation date,
// turning it possible to check when the current flow
// will "expires".
return $this->active_on->modify($relativeTime);
}
问题
检查 PHP 中的值过期很容易。问题是现在我只需要 select 直接来自数据库的 "expired" 流,我不知道是否可以使用这种方法。我怎样才能做到这一点?
您可以在 relative_time 中存储天数,而不是 php 日期间隔。这样就可以查询:
SELECT * FROM flows, steps WHERE flows.step_id = step.id AND NOW() > ADDDATE(flows.active_on, steps.relative_time)
这样你就可以让所有的流量都过期了。
实际上不需要改变数据库结构。您可以创建迁移以将 relative_time 从 dateinterval 转换为天数(是一个字符串字段)。
foreach (Steps::all() as $step) {
$step->update([
'relative_time' => strtotime($step->relative_time,0)/(3600*24);
]);
}
然后你可以调整getExpiresAtAttribute:
/**
* @return null|Carbon
*/
public function getExpiresAtAttribute()
{
if (!$this->active_on) {
return null;
}
// Access the relative time for step (for example: "+3 days").
$relativeTime = $this->step->relative_time;
// Adds the limit time to the activation date,
// turning it possible to check when the current flow
// will "expires".
return $this->active_on->modify("+$relativeTime days");
}
我有两个表:steps 和 flows。
- 步骤有很多流程。
- 步骤有
relative_time
(字符串)列。 - 流有
active_on
(可为空,时间戳)列。
步骤 relative_time
存储 relative formats,它们的值类似于:“+1 年”、“+2 天”、“+30 天”等
流程 active_on
当流程被用户激活时设置,它看起来像:“2017-12-30 00:00:00”。
每次我想知道流什么时候过期,我从数据库中select它,然后在PHP(Laravel ,Carbon), 我执行:
/**
* @return null|Carbon
*/
public function getExpiresAtAttribute()
{
if (!$this->active_on) {
return null;
}
// Access the relative time for step (for example: "+3 days").
$relativeTime = $this->step->relative_time;
// Adds the limit time to the activation date,
// turning it possible to check when the current flow
// will "expires".
return $this->active_on->modify($relativeTime);
}
问题
检查 PHP 中的值过期很容易。问题是现在我只需要 select 直接来自数据库的 "expired" 流,我不知道是否可以使用这种方法。我怎样才能做到这一点?
您可以在 relative_time 中存储天数,而不是 php 日期间隔。这样就可以查询:
SELECT * FROM flows, steps WHERE flows.step_id = step.id AND NOW() > ADDDATE(flows.active_on, steps.relative_time)
这样你就可以让所有的流量都过期了。
实际上不需要改变数据库结构。您可以创建迁移以将 relative_time 从 dateinterval 转换为天数(是一个字符串字段)。
foreach (Steps::all() as $step) {
$step->update([
'relative_time' => strtotime($step->relative_time,0)/(3600*24);
]);
}
然后你可以调整getExpiresAtAttribute:
/**
* @return null|Carbon
*/
public function getExpiresAtAttribute()
{
if (!$this->active_on) {
return null;
}
// Access the relative time for step (for example: "+3 days").
$relativeTime = $this->step->relative_time;
// Adds the limit time to the activation date,
// turning it possible to check when the current flow
// will "expires".
return $this->active_on->modify("+$relativeTime days");
}