Like in many other date functions, PHP calculates the correct value for out-of-range input, so the constructor won't necessarily throw an exception when fed with wrong dates:
<?php
date_default_timezone_set('Europe/Madrid');
try{
$d1 = new DateTime('2009-02-28');
echo $d1->format(DATE_RSS) . "\n";
}catch(Exception $e){
echo "Invalid date: 2009-02-28\n";
}
try{
$d2 = new DateTime('2009-02-29');
echo $d2->format(DATE_RSS) . "\n";
}catch(Exception $e){
echo "Invalid date: 2009-02-29\n";
}
try{
$d3 = new DateTime('2009-02-xx');
echo $d3->format(DATE_RSS) . "\n";
}catch(Exception $e){
echo "Invalid date: 2009-02-xx\n";
}
?>
Sat, 28 Feb 2009 00:00:00 +0100
Sun, 01 Mar 2009 00:00:00 +0100
Invalid date: 2009-02-xx
DateTime::__construct
(PHP 5 >= 5.2.0)
DateTime::__construct — Returns new DateTime object
Beschreibung
Returns new DateTime object.
Parameter-Liste
- time
-
String in a format accepted by strtotime(), defaults to "now".
- timezone
-
Time zone of the time.
Fehler/Exceptions
Emits Exception in case of an error.
Beispiele
Beispiel #1 DateTime::__construct() example
<?php
date_default_timezone_set('Europe/London');
$datetime = new DateTime('2008-08-03 14:52:10');
echo $datetime->format(DATE_ATOM);
?>
DateTime::__construct
alvaro at demogracia dot com
04-Aug-2009 12:09
04-Aug-2009 12:09
