drsxzut183207938 2014-03-20 08:16
浏览 53
已采纳

在php中比较从mysql到time()的时间

i need help for this. here's the scenario the link is available in 7:00AM to 7:30AM which that time are from mysql database.

StartTime = 2014-03-19 07:00:00

EndTime = 2014-03-20 07:30:00

iv'e tried converting it to strtotime() in php.

so here's my code

$date_s = new DateTime($row->StartDate);

$date_e = new DateTime($row->EndDate);

$g = date("Y-m-d H:i:s");

<?= strtotime($date_s->format('Y-m-d H:i:s')) > strtotime($g) && strtotime($date_e->format('Y-m-d H:i:s')) < strtotime($g) ? "true" : "false"; ?>

the result always fail me, im stack about 1 hour ago. can anyone help please. :D

  • 写回答

3条回答 默认 最新

  • duanmi8349 2014-03-20 08:20
    关注

    You could compare DateTime object directly:

    $date_s = new DateTime($row->StartDate);
    $date_e = new DateTime($row->EndDate);
    $now = new DateTime();
    
    if ($now > $date_s && $now < $date_e) {
    
    }
    

    Or not using DateTime object:

    $date_s = strtotime($row->StartDate);
    $date_e = strtotime($row->EndDate);
    $now = time();
    
    if ($now > $date_s && $now < $date_e) {
    
    }
    

    Using either one but not BOTH.

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

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改