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

使用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).

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

报告相同问题?

悬赏问题

  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义