doubianyan9749 2012-08-07 11:07
浏览 336
已采纳

如何将日期时间字符串转换为UTC?

How can i convert a datetime string like this: 2012-09-03 22:00 US Estern to UTC timezone?

  • 写回答

2条回答 默认 最新

  • douyi3632 2012-08-07 11:12
    关注

    You should look at the DateTime object and its related functions in the documentation.

    If your input date is already in a string format, DateTime::createFromFormat() will help you create an epoch-type integer date the object can work with. After that, it's just getTimezone() and setTimezone().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dousongqiang2585 2012-08-07 11:20
    关注

    You could convert your timestamps from DB to epoch time with strtotime and wrap them in a special CSS class :

    <span class="timestamp"><?php echo strtotime('2012-09-03 22:00'); ?></span>
    

    And then with a bit of a Javascript (jQuery example), dynamically transform the unix epoch into a locale datetime :

        $('.timestamp').each(function(){
            var seconds = $(this).html();
            var d = new Date(1000 * parseInt(seconds));
            var t = (d.getMonth()<9 ? '0':'') + parseInt(d.getMonth()+1) + '/';
            t+= (d.getDate()<10 ? '0' : '') + parseInt(d.getDate()) + '/';  
            t+= parseInt((d.getYear()<1000 ? d.getYear()+1900 : d.getYear())) + ' ';
            t+= (d.getHours()<10 ? '0' : '') + d.getHours() + ':' + (d.getMinutes()<10 ? '0' : '') + d.getMinutes();            
            $(this).html(t);
        });  
    

    This will output dates in the format mm/dd/yyyy hh:ii, relative to browser timezone.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32f103zet6 串口5无法收发数据
  • ¥15 宝塔面板的data内存要怎么清理呢
  • ¥15 关于C语言使用线程队列实现多线程并发
  • ¥15 这个运行,错误在哪里呀,大家看看吧,教导我
  • ¥15 advanceinstaller对话框设置
  • ¥100 正常上网,内部网页无法打开
  • ¥15 组件库引入并使用在若依框架未展示
  • ¥149 关于#使用python 的Flash Echarts+ajax+mysql动态数据实现饼图#的问题,请各位专家解答!
  • ¥15 RichTextBox中追加文本时报错
  • ¥15 关于c语言的学习问题