我用的是拉拉,出于某种原因,我的逻辑没有得到正确的应用。
这里的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';
}发布于 2018-05-16 15:55:17
只要使用普通的旧DateTime,它就能做你想做的事
$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用于测试它的日期。
发布于 2018-05-16 16:04:01
您可以使用碳进行比较:
如果$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()小于或等于https://stackoverflow.com/questions/50374862
复制相似问题