Following code doesn’t returns correct response
DateTime::createFromFormat('Ymd', '19992323')
returns:
date: 2000-11-23 10:01:41.000000
timezone_type: 3
timezone: America/Toronto
I was expecting it to return false. Please suggest
Following code doesn’t returns correct response
DateTime::createFromFormat('Ymd', '19992323')
returns:
date: 2000-11-23 10:01:41.000000
timezone_type: 3
timezone: America/Toronto
I was expecting it to return false. Please suggest
2
Answers
The
m
format allows number superior than 12 (php.net).That’s what happening with your date which overflows to the next year.
Example:
PHP’s
DateTime
functions and class methods are somewhat forgiving and are not meant to be used for strict validation. However, given you just have a simple year, month, and day smashed together, it is trivial to write a validation function: