douou9786 2014-09-09 10:37
浏览 42
已采纳

PHP 5.2.12 - 在DateTime对象上设置时区

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.

  • 写回答

1条回答 默认 最新

  • duanjiao8871 2014-09-09 10:57
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备