doukengsha9472 2019-07-06 13:43
浏览 151
已采纳

使用strtotime与TimeZone

I have a problem. I want to convert this DateTime: 2018-10-28 02:00:00 to a TimeStamp. Now the TimeStamp I am looking for is: 1540684800, but with my code I get this TimeStamp: 1540688400. I know it has something to do with my TimeZone, but I don't know how I can fix this. I live in the Netherlands in Amsterdam. Here is my code:

$LoopDateTime = "2018-10-28 02:00:00";
$search_key = (strtotime($LoopDateTime)*1000);

Can someone help me?

  • 写回答

1条回答 默认 最新

  • dourun2990 2019-07-07 12:45
    关注

    The time zone identifier for Amsterdam is Europe/Amsterdam and 1540688400 is the correct timestamp. There's surely an online tool to check but you can also verify it from PHP itself:

    $date = new DateTime("@1540688400");
    $date->setDateTimeZone(new DateTimeZone('Europe/Amsterdam'));
    echo $date->format('r'); // Sun, 28 Oct 2018 02:00:00 +0100
    

    However your code is not robust because depends on the configured timezone. You can just set it explicitly in a number of ways, e.g.:

    $LoopDateTime = "2018-10-28 02:00:00";
    $search_key = strtotime($LoopDateTime . ' Europe/Amsterdam') * 1000;
    var_dump($search_key); // int(1540688400000)
    

    Or:

    date_default_timezone_set('Europe/Amsterdam');
    $LoopDateTime = "2018-10-28 02:00:00";
    $search_key = strtotime($LoopDateTime) * 1000;
    var_dump($search_key); // int(1540688400000)
    

    P.S. If I'm not wrong Sunday 28 Oct 2018 02:00:00 +0100 is the exact moment when most Europe has just switched from CEST (+0200) to CET (+0100).

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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题