1

According to this calculator this date should be Mon Apr 01 2013 13:11:57 GMT+0200. But instead it's the servers time: 2013-04-01 07:11:57. I get that this is a timezone problem (the server is in -4) but is there a way to make this timezone independent or do I have to query the user's timezone from the database?

$date = 1364814717;
$date = date('Y-m-d H:i:s', $date);
erdomester
  • 11,789
  • 32
  • 132
  • 234
  • Yes you need to ask the user for it's timezone or check the IP address and geo-locate it by it's address structure. PHP can't know the visitor's timezone any other way, and even then it's based on location, which might not always be where you are (think about proxy). Also you see that all the people are giving wrong answers now because your title suggest something else to ask than your actual question states ;) –  Apr 01 '13 at 11:35
  • http://stackoverflow.com/questions/2505681/timezone-conversion-in-php Check this thread – Svetoslav Apr 01 '13 at 11:36
  • Use `gmdate()` if you want the UTC/GMT formatted output. Note that the refered calculater wroute out GMT+2. – mario Apr 01 '13 at 11:37
  • I still need the user's timezone. I store it in the database I am just asking if there is an easire way. – erdomester Apr 01 '13 at 11:38

3 Answers3

0

You can change the default time zone like this:

date_default_timezone_set('UTC');

If you need to make this different per-user, you could always send the unconverted number and use javascript (see getTimezoneOffset), but it's probably better to let the user choose (their computer might not know where it really is!)

Dave
  • 44,275
  • 12
  • 65
  • 105
  • I still need the user's timezone. I guess I cannot avoid querying the user's timezone. – erdomester Apr 01 '13 at 11:39
  • @erdomester see updated answer if javascript is an option for you. You could also try to figure it out from the IP server-side, but that will often go wrong (use of proxies for a start) – Dave Apr 01 '13 at 11:41
  • No it's ok I am using the query in other files, thanks for the answer. – erdomester Apr 01 '13 at 11:44
0

I hope this is what you are looking for.

   var myDate = new Date();
   document.write(myDate.getTimezoneOffset());
Harish
  • 1,469
  • 16
  • 43
0

You will need to ask where the user is and store it in it's profile in the database. You can't rely on anything actually. As users can be behind a proxy and seem to be like in Shang Hai, but are actually in Melbourne.

For example the user says it is in GMT +1. You put the option list in the user's profile and when the user saves it, it will be written to a database row called (for example) timezone.

Try to use the newer DateTime style (example from user with ID 7);

PHP

$mysqli = new mysqli("localhost", "my_user", "my_password", "database_name");

if ($result = $mysqli->query("SELECT timezone FROM users where user_id = 7")) {
    $prefix = '';

    if ($result[0]->timezone >= 0) {
        $prefix = '+';
    }

    $date = new DateTime('NOW GMT'.$prefix.$result[0]->timezone);

    echo '<pre>';
    print_r($date);
    echo '</pre>';
}
else {
    echo 'Query failed';
}

What happens here is you only save the GMT offset (or use whatever timeset you want) and return the current time based on the user's preference.

Output

DateTime Object (
    [date] => 2013-04-01 13:50:08
    [timezone_type] => 1
    [timezone] => +01:00
)

Don't forget to look into summertime ;)

Good luck!

  • Wow. Great approach! I rarely see this in website profiles, but today I registered at Drupal and it asked for my timezone. So far, I have relied on the user's timezone (getTimezoneOffset()). – erdomester Apr 01 '13 at 17:32