tags:

views:

26

answers:

4
date_create('sdfsdfdsfsd') //invalid
date_create('2010-07-30 08:03') //valid
+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
+1 Return of the dreaded 'new answer has been posted' bar...
BoltClock
Oops... I was too slow too
Jhong
+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
or:`try{$d = new DateTime('blargh!!!');}catch(Exception $e){echo $e->getMessage();}`
SeanJA
+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
doh! I was too slow too
SeanJA