PHP错误的strtotime结果

时间:2014-08-03 11:11:23

标签: php

时间"24:14"无效时间,"00:14"的有效时间正确,但strtotime返回CORRECT且错误。

    if(strtotime("24:14")) {
        echo "CORRECT<br/>";
    } else {
        echo "NOT CORRECT<br/>";
    }

codepad link

3 个答案:

答案 0 :(得分:0)

你很困惑。

strtotime不会返回true或false。它返回一个时间戳,这是一个真值或错误时为假。

引用手册:

  

成功时返回时间戳,否则返回FALSE。在PHP 5.1.0之前,此函数在失败时返回-1。

参考 - http://php.net/manual/en/function.strtotime.php

答案 1 :(得分:0)

因为它在出错时返回false,您需要将其与false

进行比较
if(strtotime("24:14") !== false) {
    //Correct
} else {
    //Not correct
}

答案 2 :(得分:0)

strtotime使用标准的PHP时间格式。正如您所见here&#34; 24&#34;是PHP 5.3.0中的小时有效字符串。

您可以使用regex来检查该时间,例如:

function time_check($t) {
    $reg = '/^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/';
    if ( preg_match($reg, $t) ) 
       echo "CORRENT\n";
    else
       echo "WRONG\n";
}

$check_ok = "01:22";
$check_no = "24:11";

time_check($check_ok);
time_check($check_no);