将数据库中的时间戳与过去 6 个月的时间戳进行比较
Comparing timestamp from database to last 6 months
我正在使用 laravel,但出于某种原因,我的逻辑没有正确应用。
此处的 if 语句应从数据库中获取结果,并查看 created_at 时间戳是否在最近 6 个月内。如果是这样,我在下面有一个 css class for 'newCust'。如果 created_at 时间戳超过 6 个月,则不应应用。
我正在使用 subMonth 来获取最近 6 个月的数据,但它似乎没有应用,因为现在它将 CSS class 应用于所有行,所以我想知道我的日期比较是否离开这里。
$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';
}
只使用普通的旧 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
使用的日期进行测试
可以用Carbon来比较:
如果$d
是一个模型的实例,created_at
应该已经是一个碳对象
$deadline = Carbon::now()->subMonths(6);
if($d->created_at.gt($deadline)){
// Do something
// Set the format here
}
Carbon 有多个比较函数:
eq()
等于
ne()
不等于
gt()
大于
gte()
大于等于
lt()
小于
lte()
小于或等于
我正在使用 laravel,但出于某种原因,我的逻辑没有正确应用。
此处的 if 语句应从数据库中获取结果,并查看 created_at 时间戳是否在最近 6 个月内。如果是这样,我在下面有一个 css class for 'newCust'。如果 created_at 时间戳超过 6 个月,则不应应用。
我正在使用 subMonth 来获取最近 6 个月的数据,但它似乎没有应用,因为现在它将 CSS class 应用于所有行,所以我想知道我的日期比较是否离开这里。
$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';
}
只使用普通的旧 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
使用的日期进行测试
可以用Carbon来比较:
如果$d
是一个模型的实例,created_at
应该已经是一个碳对象
$deadline = Carbon::now()->subMonths(6);
if($d->created_at.gt($deadline)){
// Do something
// Set the format here
}
Carbon 有多个比较函数:
eq()
等于ne()
不等于gt()
大于gte()
大于等于lt()
小于lte()
小于或等于