date_create('sdfsdfdsfsd') //invalid
date_create('2010-07-30 08:03') //valid
views:
26answers:
4
+5
A:
As you can see from the documentation it is simply an alias of DateTime::__construct which:
Returns a new DateTime instance. Procedural style returns FALSE on failure.
Krevan
2010-07-31 13:08:29
+1 Return of the dreaded 'new answer has been posted' bar...
BoltClock
2010-07-31 13:10:04
Oops... I was too slow too
Jhong
2010-07-31 13:11:11
+1
A:
Isn't this covered right at the top of the manual page?
<?php
$date = date_create('2000-01-01');
if (!$date) {
$e = date_get_last_errors();
foreach ($e['errors'] as $error) {
echo "$error\n";
}
exit(1);
}
echo date_format($date, 'Y-m-d');
?>
Jhong
2010-07-31 13:10:07
or:`try{$d = new DateTime('blargh!!!');}catch(Exception $e){echo $e->getMessage();}`
SeanJA
2010-07-31 13:11:22
+1
A:
If the date_create function doesn't return a valid date object, it'll return FALSE.
A much more complicated alternative is to validate its parameter with Regex according to http://www.php.net/manual/en/datetime.formats.php.
boxoft
2010-07-31 13:15:04