How to check in PHP if datetime is valid

Today I want present you an easy way to check if datetime is valid. I have tried several way from Google to do this but without success

My way is based on the php's function strtotime. If the format is not valid, strtotime will return null or current time. Then I check if the strtotime results is equal to my datetime.

Here you can see all code to check if the datetime data is valid:

$datetime = '2016-05-29 23:11:10';
if(date('Y-m-d H:i:s',strtotime($datetime)) == $datetime)
echo 'The datetime is valid.';
echo 'The datetime is not valid.';

You can see how this function works in the video bellow:

Leave a Comment