How can I check a php variable is datetime?
4 answers
This should work for you to check if the date is correct in your format:
<?php
$date = '2014-08-26 18:25:47';
function isTime($time) {
if (preg_match("/^([1-2][0-3]|[01]?[1-9]):([0-5]?[0-9]):([0-5]?[0-9])$/", $time))
return true;
return false;
}
$parts = explode(" ", $date);
list($y, $m, $d) = explode("-", $parts[0]);
if(checkdate($m, $d, $y) && isTime($parts[1]) ){
echo "OK";
} else {
echo "BAD";
}
?>
output:
OK
0
source to share
If it is a string representing a date in time, you can use date_parse: http://php.net/manual/en/function.date-parse.php This will return an associative array with the datetime string parts, if valid, FALSE. if not.
If it is a date object, see the answer describing instanceOf
.
0
source to share