dtlhy0771 2017-01-26 04:08
浏览 61
已采纳

如何在PHP中将/ Date(1490914800000 + 0100)/格式的日期转换为mysql datetime格式?

I have tried to solve it by extracting the numeric part and then parsed it using date function. But it shows me some old date which I guess is not correct.

$datef = "1490914800000+0100";
$adada = date('Y-m-d H:i:s', $datef);
// Gives date 1987-10-13 18:31:28 which is an old date. Please suggest.
  • 写回答

2条回答 默认 最新

  • doushan7997 2017-01-26 04:21
    关注

    One approach, well-covered by this SO question, is to use the DateTime() function to convert time in seconds since epoch to a date, and then display this date using format(). But there are two caveats with your data. First, you appear to have milliseconds since the epoch, which needs to be converted to seconds. Second, you also have a timezone shift, in hours, tagged to the end. I split your $datef string into two parts, epoch and timezone, then arrive at the number of seconds since epoch.

    list($epoch, $timezone) = explode('+', $datef);
    
    $epoch = ($epoch / 1000) + (substr($timezone, 0, 2)*60*60) +
             (substr($timezone, 2, 2)*60);
    $dt = new DateTime("@$epoch");
    echo $dt->format('Y-m-d H:i:s');
    

    Output:

    2017-03-31 00:00:00
    

    Demo here:

    PHP Sandbox

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!