php regex extract date, php regular expression date and time

Full Power Solutions:

function validate($str)

{undefined

preg_match('/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})$/', $str, $matches);

if (count($matches) != 7)

return false;

$valid_year = range(2000, 2050); // your range

$valid_month = range(1, 12);

$valid_day = range(1, 31);

$valid_hour = range(0, 24);

$valid_minute = range(0, 59);

$valid_second = range(0, 59);

list($str, $year, $month, $day, $hour, $minute, $second) = $matches;

foreach(array('year', 'month', 'day', 'hour', 'minute', 'second') as $part)

{undefined

if (!in_array($$part, ${'valid_'.$part}))

return false;

}

return checkdate($month, $day, $year); // this will reject absurd values like February 30 or April 31

} Edit: I don't believe in strtotime. For example: "The valid range of timestamps is usually from Friday, 13 Dec 1901 20:45:54 UTC to Tuesday, 19 Jan 2038 03:14:07 UTC." (quoted from the strtotime man page). What if I want to accept dates outside this range? For me strtotime is an unknown black box. Don't get me wrong: I will use it eventually! But I won't do it.

Related: php regex extract date, php regular expression date and time