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.

  • 写回答

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.

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

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题