php is date code example
Example 1: php time format
$today = date("F j, Y, g:i a");
$today = date("m.d.y");
$today = date("j, n, Y");
$today = date("Ymd");
$today = date('h-i-s, j-m-y, it is w Day');
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');
$today = date("D M j G:i:s T Y");
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');
$today = date("H:i:s");
$today = date("Y-m-d H:i:s");
Example 2: php check if format is date
function validateDate($date, $format = 'Y-m-d')
{
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) === $date;
}
Test cases:
var_dump(validateDate('2013-13-01'));
var_dump(validateDate('20132-13-01'));
var_dump(validateDate('2013-11-32'));
var_dump(validateDate('2012-2-25'));
var_dump(validateDate('2013-12-01'));
var_dump(validateDate('1970-12-01'));
var_dump(validateDate('2012-02-29'));
var_dump(validateDate('2012', 'Y'));
var_dump(validateDate('12012', 'Y'));
Example 3: date now php
date("Y-m-d H:i:s");