Is the string valid for use with strtotime ()?

PHP has strtotime(), which assumes that the input you enter is a string that represents the actual time. Before use strtotime(), how can I verify that the string that should be provided to it is in one of its valid formats?

+5
source share
2 answers

strtotime()returns false if it cannot understand your date format, so you can check its return value. The function has no side effects, so trying to call it will not hurt.

// $format = ...

if (($timestamp = strtotime($format)) !== false) {
    // Obtained a valid $timestamp; $format is valid
} else {
    // $format is invalid
}

, , strtotime() Unix, . , false . manual:

, , Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. ( , 32- .) , , , Unix. , , , 1 1970 . Windows, Linux . PHP 5.1.0 .

64- PHP , 64 293 .

, 32- Unix , 64- .

+10

, php.net. , , , "false".

strtotime().

, strtotime().

+2

All Articles