Derick confirmed, that the following is expected behaviour:
<?php
var_dump( date_default_timezone_get() );
$dateTime = new DateTime( '23:01' );
var_dump( $dateTime->getTimezone()->getName() );
$dateTime = new DateTime( '@1234567890' );
var_dump( $dateTime->getTimezone()->getName() );
?>
Output:
string(13) "Europe/Berlin"
string(13) "Europe/Berlin"
string(6) "+00:00"
So depending on the format you use to define the DateTime object it either receives the default timezone or not.
DateTime::__construct
(PHP 5 >= 5.2.0)
DateTime::__construct — Returns new DateTime object
Description
Returns new DateTime object.
Parameters
- time
-
String in a format accepted by strtotime(), defaults to "now".
- timezone
-
Time zone of the time.
Errors/Exceptions
Emits Exception in case of an error.
Examples
Example #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
thomas at koch dot ro
17-Nov-2009 06:26
17-Nov-2009 06:26
alvaro at demogracia dot com
04-Aug-2009 03:09
04-Aug-2009 03:09
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
