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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)