简体   繁体   中英

PHP 5.2.12 - Setting the Time Zone on a DateTime Object

As usual, I have read the manual page for setTimeZone and Googled for any previous solutions, but so far, no luck.

I have two servers, one running PHP 5.3 (Server A) and one running PHP 5.2 (Server B). PHPinfo for both shows that date/time support is enabled and there is a Timezone Database.

However, when migrating a script from Server A to Server B, I started getting an error message

Fatal error: Call to a member function format() on a non-object

referring to

$startdate = $startdate->format("c");

To test what was going wrong, I ran the following script on both servers:

$startdate = '2014-05-05 10:00:00';
echo "Start: " . $startdate ."<br/>";

$tzone = new DateTimeZone('Europe/London');
echo "Tzone: " . print_r($tzone, true) ."<br/>";

$dateobj = new DateTime($startdate, $tzone);
echo "Date Obj: " . print_r($dateobj, true) ."<br/>";

$formatted = $dateobj->format("c");
echo "Formatted: " . print_r($formatted, true) ."<br/>";

$utzone = new DateTimeZone('UTC');
echo "UTzone: " . print_r($utzone, true) ."<br/>";

$utc = $dateobj->setTimeZone($utzone);
echo "UTC: " . print_r($utc, true) ."<br/>";

$formatted2 = $utc->format("c");
echo "Formatted UTC: " . print_r($formatted2, true) ."<br/>";

From Server A (PHP 5.3) I get

Start: 2014-05-05 10:00:00
Tzone: DateTimeZone Object ( ) 
Date Obj: DateTime Object ( [date] => 2014-05-05 10:00:00 [timezone_type] => 3 [timezone] => Europe/London ) 
Formatted: 2014-05-05T10:00:00+01:00
UTzone: DateTimeZone Object ( ) 
UTC: DateTime Object ( [date] => 2014-05-05 09:00:00 [timezone_type] => 3 [timezone] => UTC ) 
Formatted UTC: 2014-05-05T09:00:00+00:00 

From Server B (PHP 5.2) I get

Start: 2014-05-05 10:00:00
Tzone: DateTimeZone Object ( ) 
Date Obj: DateTime Object ( ) 
Formatted: 2014-05-05T10:00:00+01:00
UTzone: DateTimeZone Object ( ) 
UTC: 

Fatal error: Call to a member function format() on a non-object 

The fact that the Formatted line contains a string shows that the DateTime object is populated and the the blank print_r is, I think, due to a documented problem with using print_r on datetime in PHP5.2.

However, as far as I can tell from the documentation, setDateTimeZone is supported in PHP5.2 so I can't work out why this wouldn't be working on Server B.

Can anyone advise? If it's of any relevance, Server A is running Apache and Server B IIS.

Interesting, yet quite simple to explain: As explained in the DateTime docs , the return value of setTimezone was null until PHP 5.3.

In your code, you assign the result of setTimezone to the $utc variable. In PHP 5.3, $utc is now the DateTime object again, in PHP 5.2 it's null . In the following line, you try to call the format method on $utc , which of course must fail in PHP 5.2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM