doumi7854 2018-05-16 15:31
浏览 267
已采纳

将数据库的时间戳与过去6个月进行比较

I'm using laravel and for some reason my logic here isn't applying correctly.

The if statement here should take the results from the database and see if the created_at timestamp is within the last 6 months. If so, I have a css class for 'newCust' below. If the created_at timestamp is past 6 months, it should not be applied.

I'm using subMonth to get the last 6 months but it seems like it's not applying because now it's applying the CSS class to all rows so I'm wondering if my date comparison is off here.

$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';
}
  • 写回答

2条回答 默认 最新

  • dongshanjin8947 2018-05-16 15:55
    关注

    Using just plain old DateTime this will do what you want

    $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';
    }
    

    Adjust the date used by $createdAt to test it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?