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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀