【发布时间】:2018-05-25 00:07:36
【问题描述】:
我正在使用 laravel,由于某种原因,我的逻辑无法正确应用。
这里的 if 语句应该从数据库中获取结果,并查看 created_at 时间戳是否在最近 6 个月内。如果是这样,我在下面有一个用于“newCust”的 css 类。如果 created_at 时间戳超过 6 个月,则不应应用。
我正在使用 subMonth 来获取过去 6 个月,但它似乎不适用,因为现在它将 CSS 类应用于所有行,所以我想知道我的日期比较是否在这里关闭。
$d->createdAt = new \DateTime($d->created_at); // created_at is full timestamp (2011-01-01 00:00:00)
$deadline = Carbon::now()->subMonths(6)->format('Y-m-d H:i:s');
if($d->createdAt > $deadline){ // I'm trying to say here that if the created_at timestamp is within the last 6 months, then apply following logic
$d->call_status = 'newCust';
}
【问题讨论】:
-
为什么不对这两个时间戳使用
Carbon?您正在混合\DateTime和Carbon,这可能会影响比较逻辑。 -
您还将 DateTime 对象与字符串进行比较,这可能会使事情变得混乱。 Carbon 有 comparison functions,所以就用它吧。
标签: php laravel php-carbon