【问题标题】:PHP: How to compare a time string with date('H:i')?PHP:如何将时间字符串与日期('H:i')进行比较?
【发布时间】:2012-05-16 21:30:46
【问题描述】:

我有时间作为 varchar 字段保存在数据库中,例如晚上 7:30。我想检查这个时间是否大于现在的时间。

我将 DB 时间字符串转换为“19:30”,现在我想做这样的事情:

$my_time = '19:30';

if($my_time  > date('H:i'))
{
    do something ...
}

问题是如果 $my_time 是非空字符串,上述将始终返回 true。

strtotime($my_time) 也无济于事。

strtotime('H:i',$my_time) 00:00 。

当实际时间是 17:09 时,(int)date('H:i') 将给出 1700,因此删除冒号然后比较也不会起作用....

在这种情况下更改数据库时间数据是不可能的。

请帮忙。如果我说错了一些事实,请纠正我。

【问题讨论】:

标签: php datetime comparison


【解决方案1】:

你可以用这个:

$myTime = '19:30';
if (date('H:i') == date('H:i', strtotime($myTime))) {
    // do something
}

【讨论】:

  • 已经过去了很多时间,但是这和直接比较两个字符串不一样吗?由于date() 返回一个字符串,它完全一样:if(date('H:i') == '19:30') 不是吗?,这与 OP 提到的尝试相同。那是因为date('H:i', strtotime($myTime)) 将原始 $myTime 值作为字符串返回!!!所以这与只输入$myTime 而不是date('H:i', strtotime($myTime)) 相同。那么有什么意义呢?我真的不明白。我错过了什么?为什么 OP 建议的 if($my_time > date('H:i')) 据说不起作用?因为它确实对我有用。
  • 问题涉及如何比较一个是否大于第二个。不仅仅是相等比较。
【解决方案2】:

您可以构造一个新的DateTime 对象,将时间设置为随机日期。比比较这两个对象。例如:

$my_time = new DateTime('January 1th 1970 19:30');
$comparable_time = new DateTime('January 1th 1970 '. date('H:i'));
if($my_time < $comparable_time) {
    // do something
} else {
    // do something else
}

请注意更新日志;

Version 5.2.2    DateTime object comparison with the comparison operators changed to work as expected. Previously, all DateTime objects were considered equal (using ==).

【讨论】:

    【解决方案3】:

    你不能对这样的字符串使用比较运算符,因为当你做字符串get converted to numbers first时。

    对于单行解决方案,您可以使用strcmp

    if(strcmp($my_time, date('H:i')) == 1)
    {
        do something ...
    }
    

    上述条件在语义上等价于“如果 $my_time 大于当前时间”,但前提是字符串的格式保持一致! 很容易引入bug在此代码中,如果出于某种原因$my_time 的格式不直接对应于H:i 模式。

    将值简化为字符串通常不是使用日期和时间的方式。更合适的解决方案是使用 PHP 5.2.0 中引入的原生 DateTime 类(John Conde 已经在 his answer 中给出了示例)。

    但是,将时间视为愚蠢的标量值还有一个可能的优势:结果与人类的看法一致,即 01:00 总是晚于 00:00。 DateTime 方法取决于当地时区和日期,可能并不总是给您预期的结果。示例:

    // assume we are in London
    date_default_timezone_set('Europe/London');
    
    // assume that today is March 25, 2012
    $date1 = new DateTime("2012-03-25 01:00:00");
    $date2 = new DateTime("2012-03-25 02:00:00");
    
    // and...
    if ($date1 == $date2) {
        echo "WTF?!? Equal???";
    }
    

    See it in action.

    此测试的结果与比较“01:00”和“02:00”的某些标量表示的结果不同,因此最好考虑一下比较的正确语义是什么。

    【讨论】:

    • 它与印度、加尔各答时区一起工作。我想我可以在实时服务器上使用它。
    【解决方案4】:
    $date1 = DateTime::createFromFormat('H:i', $my_time1);
    $date2 = new DateTime();
    if ($date1 > $date2)
    {
         // do something   
    }
    

    【讨论】:

    • +1,以及强制性免责声明:根据当前日期和时区的具体值,此测试的结果可能会令人惊讶。
    • 我收到以下错误:致命错误:未捕获的异常 'Exception' 带有消息 'DateTime::__construct() [datetime.--construct ]: 无法解析位置 1 (:) 的时间字符串 (H:i): Unexpected character' in C:\wamp\www\workspace\bigbite\index_parallel.php on line ...... .......... 后面还有警告: Exception: DateTime::__construct() [datetime.--construct] : 无法在位置 1 (:) 解析时间字符串 (H:i):C:\wamp\www\workspace\bigbite\index_parallel.php 中出现意外字符
    【解决方案5】:

    不要比较代表时间戳的字符串。相反,使用 strtotime() 将任何此类字符串转换为 Unix 时间戳,它们只是数字,然后比较它们。您可以使用time() 获取当前时间的 Unix 时间戳:

    $my_time = '19:30';
    
    if (strtotime($my_time) > time()) {
        // do something ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      • 2022-01-16
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      相关资源
      最近更新 更多