我正在使用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 来比较:
如果$d
是模型的实例,created_at
应该已经是碳对象
$deadline = Carbon::now()->subMonths(6);
if($d->created_at.gt($deadline)){
// Do something
// Set the format here
}
碳具有多种比较功能:
eq()
等于ne()
不等于- 大于
gt()
- 大于或等于
gte()
lt()
小于lte()
小于或等于
使用普通的旧日期时间,这将执行您想要的操作
$createdAt = new DateTime('2017-11-17 00:00:00');
echo $createdAt->format('d/m/Y H:i:s') . PHP_EOL;
$deadline = new DateTime();
$deadline->sub( new DateInterval('P6M') );
echo $deadline->format('d/m/Y H:i:s') . PHP_EOL;
if ($createdAt > $deadline) {
echo 'YES';
}else{
echo 'NO';
}
调整$createdAt
用于测试它的日期。