PHP date_create_from_format function is accepting non-existent, though format-valid dates.
I would like this function to behave like the date command:
niloct@HP-Mini:~$ date --date="29/02/2011" +%s
date: invalid date `29/02/2011'
though this is what happens in php:
$tmp = date_create_from_format('d/m/Y H:i:s',"29/02/2011 00:00:00", timezone_open('America/Sao_Paulo'));
var_dump($tmp);
/*
output: object(DateTime)#28 (3) { ["date"]=> string(19) "2011-03-01 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(17) "America/Sao_Paulo" }
*/
Can this automatic conversion be avoided and the function return -1 in this case ?
niloct@HP-Mini:~$ php -v
PHP 5.3.3-1ubuntu9.3 with Suhosin-Patch (cli) (built: Jan 12 2011 16:08:14)
Thank you.
I found that calling
date_get_last_errorsafterdate_create_from_formatalready gives you all kind of validations on the parsed string, in the arrayswarningsanderrors, and counterswarning_countanderror_count.So just checking the counters for zero warnings and errors is enough.
Revised code with some tests:
The above script calculates the unix timestamp of the datetime created.