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.