doufen1890
2015-06-05 18:31
浏览 15
已采纳

使用php转换时区?

How do I convert the timestamp 2015-06-05 14:05:01 to another timezone using php?

I've read and tried numerous ways listed on here but I cannot get the desired result. Using date_format($date,"M d h:i A") and date_default_timezone_set('America/New_York') I get June 05 2:05 PM which is the original origin of the server timezone and correct.

What I need is to convert 2015-06-05 14:05:01 using for example date_default_timezone_set('America/Los_Angeles') and date_format($date,"M d h:i A") to get the result June 05 11:05 AM.

图片转代码服务由CSDN问答提供 功能建议

如何将时间戳 2015-06-05 14:05:01 转换为 另一个使用php的时区?

我已阅读并尝试了多种方法,但我无法获得理想的结果。 使用 date_format($ date,“M dh:i A”) date_default_timezone_set('America / New_York')我得到 June 05 2:05 PM 这是服务器时区的原始来源并且正确。

我需要的是转换 2015-06-05 14:05:01 ,例如 date_default_timezone_set('America / Los_Angeles') date_format($ date,“M dh:i A”)得到结果 June 05 11:05 AM

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujuegai8830 2015-06-05 18:34
    已采纳

    Use DateTime() with DateTimeZone():

    // Create the DateTime() object and set the timezone to 'America/New_York'
    $date = new DateTime('2015-06-05 14:05:01', new DateTimeZone('America/New_York'));
    
    // Change the timezone to 'America/Los_Angeles'
    $date->setTimezone(new DateTimeZone('America/Los_Angeles'));
    
    // Print out the date and time in the new timezone
    echo $date->format('M d h:i A');
    

    Demo

    Easy to read which makes it easy to mantain.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题