Does anyone know if there is a built in or quick function to check if a string is a MySQL datetime format? Here is an example:
2038-01-19 03:14:07
Does anyone know if there is a built in or quick function to check if a string is a MySQL datetime format? Here is an example:
2038-01-19 03:14:07
You can try DateTime::createFromFormat('Y-m-d H:i:s', '2038-01-19 03:14:07')
and see if it returns false. http://www.php.net/manual/en/datetime.createfromformat.php
I loved the answer by epicdev, however, the class seems to only validate the format, a date like 2015-18-39 is still valid for it and is converted to 2016-07-09 instead of rejecting the invalid day/month A slight change to it is to double check that the date parsed is still the same as the date entered.
function proposed by glavic at gmail dot com on php.net documentation
function validateDate($date, $format = 'Y-m-d H:i:s')
{
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}