Why does strtotime give different result in different timezone?

11,069

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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

Share:
11,069
Muhammad Usman
Author by

Muhammad Usman

I love creating complex web applications. PHP (with Laravel) is my passion. Programming since 2001, I love my work and I think that is why people love it. Started programming with VB6 as a hobby while I was in Class IX (2001). In 2007 I started learning PHP after that in 2008 I joined a local software company and built various accounting and business solutions in Microsoft Technologies also worked on few PHP Projects. From 2009 I started building complex web apps in PHP as a freelancer. Now I have number of applications in my portfolio. Basically I love working in the LAMP stack.

Updated on June 15, 2022

Comments

  • Muhammad Usman
    Muhammad Usman almost 2 years

    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?

    • jamil ahmed
      jamil ahmed over 12 years
      Because the time is different in different time zones?
    • Jared Farrish
      Jared Farrish over 12 years
      Can you provide some code for what you're trying to do?
    • Muhammad Usman
      Muhammad Usman over 12 years
      @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?
    • jamil ahmed
      jamil ahmed over 12 years
      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.
    • Muhammad Usman
      Muhammad Usman over 12 years
      @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.
  • Muhammad Usman
    Muhammad Usman over 12 years
    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?
  • Muhammad Usman
    Muhammad Usman over 12 years
    +1 for mktime() but it takes parameter in several parts which is a pain :(
  • Muhammad Usman
    Muhammad Usman over 12 years
    Thanks, I have updated the question with an example, please have a look.
  • Muhammad Usman
    Muhammad Usman over 12 years
    Thanks, I have updated the question with an example, please have a look.
  • Muhammad Usman
    Muhammad Usman over 12 years
    Thanks, I have updated the question with an example, please have a look.