获取特定时区的 unix 时间戳

Get a unix timestamp in a specific timezone

我目前正在尝试获取 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.

By definition:

Unix time (also known as POSIX time or Epoch time) is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.

并且来自 strtotime documentation:

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC).

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

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