FireEmerald
FireEmerald

Reputation: 1370

Get a unix timestamp in a specific timezone

I'm currently trying to get the timestamp of the date_default_timezone_set("Europe/Berlin"); as unix timestamp, but it always results in a UTC unix timestamp.

// 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.

Upvotes: 1

Views: 783

Answers (2)

mister martin
mister martin

Reputation: 6252

You're getting a different timestamp than the one you expect, because you're not specifying a timezone. You can get the results you're after with the following change to your code:

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

Upvotes: 1

Matt S
Matt S

Reputation: 15374

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.

And from the 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).

Upvotes: 1

Related Questions