0

time "24:14" is not valid time and this should be "00:14" for correct valid time but strtotime return CORRECT and its wrong.

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

codepad link

DolDurma
  • 15,753
  • 51
  • 198
  • 377

3 Answers3

0

You're confused.

strtotime doesn't return true or false. it returns a timestamp, which is a truthy value or false on error.

quoting from the manual:

Returns a timestamp on success, FALSE otherwise. Previous to PHP 5.1.0, this function would return -1 on failure.

Reference -- http://php.net/manual/en/function.strtotime.php

Ties
  • 5,726
  • 3
  • 28
  • 37
r3wt
  • 4,642
  • 2
  • 33
  • 55
0

Because it returns false on error, you need to compare it to false

if(strtotime("24:14") !== false) {
    //Correct
} else {
    //Not correct
}
Anton
  • 542
  • 1
  • 6
  • 16
0

strtotime uses standard PHP time format. As you can see here "24" is a valid string for hours from PHP 5.3.0.

You can use a regex to check that time, for example:

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);
Community
  • 1
  • 1
ocirocir
  • 3,543
  • 2
  • 24
  • 34
  • for `00:00:01` or `00:00:00` return false – DolDurma Aug 03 '14 at 11:48
  • 1
    Yes, check the link, it's only for HH:MM format, you can simply add also seconds: ^([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]|[0-5][0-9]:[0-5][0-9])$ – ocirocir Aug 03 '14 at 11:51
  • It's already optional, I change [0-5][0-9] in ([0-5][0-9]|[0-5][0-9]:[0-5][0-9]) ;-) – ocirocir Aug 03 '14 at 12:06
  • thanks sir. how to check time is `time 12` or `time 24` in your this pattern? can i seperator this action for detect time is 12 or 24? – DolDurma Aug 03 '14 at 12:13