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.

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

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题