Jump to content

Current time in different timezones


RobertP

Recommended Posts

has anyone attempted this?

 

php version:5.4.3

 

function getTimeByZone($zone){
$default = ini_get('date.timezone');
ini_set('date.timezone',$zone);
$time = time();
ini_set('date.timezone',$default);
return $time;
}

 

echo getTimeByZone('America/Toronto').'<br />';
echo getTimeByZone('Europe/Amsterdam').'<br />';
echo getTimeByZone('GMT-8').'<br />';

 

outputs all the same.

 

ps: i know i should use http://php.net/manual/en/book.datetime.php, but i cant seem to get it working properly, same output as above.

$date->setTimezone(new DateTimeZone('GMT-8') );

 

Link to comment
Share on other sites

time() is the number of seconds since the beginning of 1970 local time. As such it is not affected by timezones. What is affected is what you get when you convert that number into a date string.

 

Also, use date_default_timezone_get/set() if you go procedural.

function getTimeByZone($zone){
$old = date_default_timezone_get();
date_default_timezone_set($zone);
$time = date('r');
date_default_timezone_set($old);
return $time;
}

Link to comment
Share on other sites

Edit: basically says the same as what requinix posted above ^^^

 

The time function returns the current Unix timestamp. Time uses the computer's/server's clock and timezone setting (if you change your computer's clock or timezone setting so that they are it is wrong for your location, you will get the timestamp based on wherever that wrong time/timezone corresponds to.) Edit2: or I could just be wrong about everything I wrote here concerning changing the timezone giving a different timestamp.

 

Only functions like date, strtotime, mktime, and a few others, actually use php's timezone setting.

Link to comment
Share on other sites

i think i got it working? can someone test aswell please?

 

function getTimeByZone($zone){
$default = date_default_timezone_get();
date_default_timezone_set($zone);
$time = time()+date('Z');
date_default_timezone_set($default);
return $time;
}

Link to comment
Share on other sites

Like I said, timestamps have nothing to do with timezones. It's the same number everywhere. Like right now it's 1339523573 (give or take) regardless of where you are. That corresponds to 10:52am PDT and 1:52pm EDT. Let me be even more explicit:

printf("Timezone:       %s\n", date_default_timezone_get());
printf("Unix timestamp: %u\n", time());
printf("Time:           %s\n", date("Y-m-d H:i:s"));
printf("\n");

date_default_timezone_set("UTC");
printf("Timezone:       %s\n", date_default_timezone_get());
printf("Unix timestamp: %u\n", time());
printf("Time:           %s\n", date("Y-m-d H:i:s"));
printf("\n");

date_default_timezone_set("Asia/Shanghai");
printf("Timezone:       %s\n", date_default_timezone_get());
printf("Unix timestamp: %u\n", time());
printf("Time:           %s\n", date("Y-m-d H:i:s"));

Timezone:       America/Los_Angeles
Unix timestamp: 1339523573
Time:           2012-06-12 10:52:53

Timezone:       UTC
Unix timestamp: 1339523573
Time:           2012-06-12 17:52:53

Timezone:       Asia/Shanghai
Unix timestamp: 1339523573
Time:           2012-06-13 01:52:53

 

Your problem is you're using the wrong timezone for something. You should never adjust a time() value to use a different timezone: use date_default_timezone_set() to change the timezone then date() to get a date string. (Or the OOP equivalent.)

Link to comment
Share on other sites

function getTimeByZone($zone){
$default = date_default_timezone_get();
$defaultOffset = date('Z');
date_default_timezone_set($zone);
$offset = date('Z');
date_default_timezone_set($default);
return ($offset-$defaultOffset)+time();
}

echo date('d-m-Y h:i:s a',getTimeByZone('America/Toronto')).'<br />';
echo date('d-m-Y h:i:s a',getTimeByZone('Europe/Lisbon')).'<br />';
echo date('d-m-Y h:i:s a',getTimeByZone('Asia/Tokyo')).'<br />';
echo date('d-m-Y h:i:s a',getTimeByZone('Pacific/Samoa')).'<br />';
exit;

 

results:
17-06-2012 09:15:24 pm
18-06-2012 02:15:24 am
18-06-2012 10:15:24 am
17-06-2012 02:15:24 pm

Link to comment
Share on other sites

No. Are you not paying attention? Sticking with your code and just fiddling with it until it works?

function dateByZone($format, $zone){
$default = date_default_timezone_get();
date_default_timezone_set($zone);
$string = date($format);
date_default_timezone_set($default);
return $string;
}

echo dateByZone('d-m-Y h:i:s a', 'America/Toronto') . '
';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.