首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php比较时间

问php比较时间
EN

Stack Overflow用户
提问于 2012-08-22 07:48:22
回答 1查看 428关注 0票数 1

我有下面的php代码,使用strtotime将两次与当前时间进行比较:

代码语言:javascript
复制
 $timingsfirstTime[0] = date("H:i:s", strtotime(trim($showTimings[0])));
 $timingslastTime[2] = date("H:i:s", strtotime(trim($showTimings[2])));

//确认第一次播放的开始时间大于频道最后一次播放的时间

代码语言:javascript
复制
        $current_time = date("H:i:s",strtotime('now'));

        $this->assertTrue(($current_time > $timingsfirstTime[0] && $current_time < $timingslastTime[2]),"current time ".$current_time. " is not greater than current show start time ". $timingsfirstTime[0] . " or current time is not less than current show end time ".$timingslastTime[2]); 

但我的断言不知何故失败了,并输出:

当前时间00:38:45不大于当前节目开始时间23:50:00或当前时间不小于当前节目结束时间00:50:00

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-22 08:02:40

您正在进行字符串比较,而不是日期比较,这就是它“失败”的原因。

改为使用DateTime,因为它更容易阅读,代码更少,并且可以进行本机比较。我还会将您的断言分成两个断言,以便更容易地判断哪个案例失败:

代码语言:javascript
复制
$now = new DateTime();
$start = new DateTime($showTimings[0]);
$end = new DateTime($showTimings[2]);

$this->assertTrue(
    $now > $start,
    'current time ' . $now->format('H:i:s')
        . ' is not greater than current show start time '
        . $start->format('H:i:s')
);

$this->assertTrue(
    $now < $end,
    'current time ' . $now->format('H:i:s')
        . ' is not less than current show end time '
        . $end->format('H:i:s')
);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12064619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档