Reputation: 99
I am working with php and mysql to develop my application. For this application, time plays an important role as I want to show data to my users according to their selected timezone. For this i am using strtotime to convert time into numeric form, but I just noticed that strtotime returns same value for all timezones. I wrote following code to test.
date_default_timezone_set("Asia/Kolkata");
echo date("Y-m-d H:i:s")." ------ ";
echo strtotime(date("Y-m-d H:i:s"))."<br/><br/>";
date_default_timezone_set("America/New_York");
echo date("Y-m-d H:i:s")." ------ ";
echo strtotime(date("Y-m-d H:i:s"));
But output is
2013-01-28 15:40:11 ------ 1359367811
2013-01-28 05:10:11 ------ 1359367811
Why is the return value identical?
Upvotes: 2
Views: 1022
Reputation: 20220
As @Bobby's comment states, "Unix Timestamps are always UTC". This means you're essentially making two equivalent conversions to the UTC timezone.
Consider that a t1 timestamp in a1 timezone and the same t1 timestamp in a2 would produce the same result when converted to timezone a3. In your example, the "same result" is in seconds, but the principle is the same.
In other words, in your code you are generating two UNIX timestamps from seemingly two different dates. But what you're actually doing is generating two different but equivalent representations (Asian and American timezones) of the same point in time. You then convert the two equivalent representations to the same third represntation (UTC timezone).
Upvotes: 1
Reputation: 532
As per PHP manual for strtotime
,
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), relative to the timestamp given in now, or the current time if now is not supplied.
As stated above, strtotime converts a given date format into a Unix timestamp, which is calculated relative to the Unix epoch. The epoch is a fixed point whose definition is independent of the user's timezone, and the number of seconds since then is, relatively, the same in all time zones.
Setting the timezone merely affects the interpretation of the timestamp value.
Upvotes: 0
Reputation: 29769
strtotime()
returns "the number of seconds since January 1 1970 00:00:00 UTC"
Upvotes: 4