【问题标题】:Get a unix timestamp in a specific timezone获取特定时区的 unix 时间戳
【发布时间】:2016-08-23 17:09:47
【问题描述】:

我目前正在尝试将 date_default_timezone_set("Europe/Berlin"); 的时间戳作为 unix 时间戳,但它始终会生成 UTC unix 时间戳。

// Current time: "2016-04-28 20:37:20"

date_default_timezone_set("Europe/Berlin");
echo date('Y-m-d H:i:s');
// -> "2016-04-28 20:37:20"

echo strtotime(date('Y-m-d H:i:s'));
// -> 1461868642 which is Thu, 28 Apr 2016 18:37:22 GMT
// i need here 1461875840 which is the current time.

【问题讨论】:

  • 时间戳,根据定义,采用 UTC。

标签: php timezone unix-timestamp


【解决方案1】:

您得到的时间戳与您预期的不同,因为您没有指定时区。通过对代码进行以下更改,您可以获得所需的结果:

echo strtotime(date("Y-m-d H:i:s") . " GMT");

【讨论】:

    【解决方案2】:

    By definition:

    Unix 时间(也称为 POSIX 时间或纪元时间)是用于描述时间瞬间的系统,定义为自 1 月 1 日星期四 00:00:00 协调世界时 (UTC) 以来经过的秒数1970 年。

    还有来自strtotimedocumentation

    该函数期望得到一个包含英文日期格式的字符串,并将尝试将该格式解析为 Unix 时间戳(自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 2012-05-06
      相关资源
      最近更新 更多