Why does strtotime give different result in different timezone?
Asked Answered
T

6

6

I am not sure why strtotime() in PHP returns different result in different timezone even though same date is given as parameter, does anyone know the answer? I also want to know, can I do similar task (converting a datetime to an int to do calculations easily) with another function which gives same result across different timezone?

EDIT:

An example: If I use strtotime('2011-09-19 00:00:00') shouldn't it just return the difference between 'January 1 1970 00:00:00' and '2011-09-19 00:00:00' in seconds ? Why timezone is an issue here? And can I get something which gives just difference without timezone issue?

Thaw answered 3/9, 2011 at 18:55 Comment(5)
Because the time is different in different time zones?Clarenceclarenceux
Can you provide some code for what you're trying to do?Juxtapose
@jeffamaphone I give the same datetime as parameter, shouldn't it covert it to an int, what to do with timezone, that's I want to know?Thaw
Yeah, I'm no expert, but I suspect it expects you're giving it some UTC time or something and is converting for you. I'm sure there is a way to prevent this.Clarenceclarenceux
@Jared Farris thanks, but I don't have problem in code. I face problem when I move my app to a server with different timezone.Thaw
S
7

In short: time zone is considered because the Unix Epoch value is considered in GMT.

In broader sense 2011-09-19 00:00:00 comes to Bangladesh almost after 6 hours it is 2011-09-19 00:00:00 in GMT zone. Because of this gap, another 21600 seconds have passed in the GMT zone when the same date appears in BD.

Since the calculation is done in respect to the GMT, you have to add these 21600 seconds to get the actual difference.

Sciomachy answered 4/9, 2011 at 20:13 Comment(0)
G
6

strtotime gives different results in different timezones because it takes timezones into account...

From strtotime's manual:

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)

This function will use the TZ environment variable (if available) to calculate the timestamp. Since PHP 5.1.0 there are easier ways to define the timezone that is used across all date/time functions. That process is explained in the date_default_timezone_get() function page.

Have a look at mktime().

Since PHP 5.1, you can use date_default_timezone_set before calling mktime or strtotime.

Germain answered 3/9, 2011 at 18:59 Comment(2)
Thanks, I am just curious, if I use strtotime('2011-09-19 00:00:00') shouldn't it just return the difference between 'January 1 1970 00:00:00' and '2011-09-19 00:00:00' ? Why timezone is an issue here? And can I get something which gives just difference?Thaw
+1 for mktime() but it takes parameter in several parts which is a pain :(Thaw
O
1

From the PHP manual:

This function will use the TZ environment variable (if available) to calculate the timestamp. Since PHP 5.1.0 there are easier ways to define the timezone that is used across all date/time functions. That process is explained in the date_default_timezone_get() function page.

http://php.net/manual/en/function.strtotime.php

Obadiah answered 3/9, 2011 at 19:0 Comment(1)
Thanks, I have updated the question with an example, please have a look.Thaw
S
1

Use date_default_timezone_set before calling date/time functions to choose which time zone you want to work in.

http://www.php.net/manual/en/function.date-default-timezone-set.php

Sackey answered 3/9, 2011 at 19:0 Comment(1)
Thanks, I have updated the question with an example, please have a look.Thaw
F
1

From PHP docs on strtotime:

This function will use the TZ environment variable (if available) to calculate the timestamp. Since PHP 5.1.0 there are easier ways to define the timezone that is used across all date/time functions. That process is explained in the date_default_timezone_get() function page.

Try setting your own time zone.

Federate answered 3/9, 2011 at 19:0 Comment(1)
Thanks, I have updated the question with an example, please have a look.Thaw
V
0

i think probably there will be one time of each php programmer that this function will make him wants to really understand how actually php works with date and time functions.

funny this function when you try something like...err...for example, assume that today at this very moment is July 11th, 2012 at 13:00:00 (2012-07-11 13:00:00) and then you try strtotime to find exactly the same moment of the day but for tomorrow:

$x = strtotime('2012-07-12 13:00:00');

$y = strtotime('+1 Day');

$z = $x-$y;

$x and $y of the above first 2 lines will not return the same thing even you ignore the minute and second counts but $z will be around 25200 or around 7 hours in the difference between this 2 lines if your sever is in somewhere of the USA that the GMT is -5 hours but you browser calls this function from berlin in summer where the GMT is +2 hours... LOL now you can get the idea how php work with this function ;)

Valedictorian answered 29/6, 2012 at 1:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.