Jump to content

Recommended Posts

I started implementing Timezones into my site yesterday. I've come across a few problems:

 

The problem seems to be that my server's time (GMT -7:00) is 1-hour ahead of the calculated GMT -07:00 time. At the time of this writing the calculated time for (GMT -07:00) was 9:47 am but my server's time is 10:47 am. The calculated Zulu-Time is correct (according to google). My server's timezone is currently set to Mountain Daylight Time, not Mountain Standard Time. I'm not quite sure what the problem is.

 

<?php
$_TZ = array(
	-12 => '(GMT -12:00) Eniwetok, Kwajalein',
	-11 => '(GMT -11:00) Midway Island, Samoa',
	-10 => '(GMT -10:00) Hawaii',
	-9 => '(GMT -9:00) Alaska',
	-8 => '(GMT -8:00) Pacific Time (US & Canada), Tijuana',
	-7 => '(GMT -7:00) Mountain Time (US & Canada), Arizona',
	-6 => '(GMT -6:00) Central Time (US & Canada), Mexico City, Central America',
	-5 => '(GMT -5:00) Eastern Time (US & Canada), Bogota, Lima, Quito',
	-4 => '(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz, Santiago',
	-3.5 => '(GMT -3:30) Newfoundland',
	-3 => '(GMT -3:00) Brasilia, Buenos Aires, Georgetown, Greenland',
	-2 => '(GMT -2:00) Mid-Atlantic, Ascension Islands, St. Helena',
	-1 => '(GMT -1:00) Azores, Cape Verde Islands',
	0 => '(GMT) Casablanca, Dublin, Edinburgh, Lisbon, London, Monrovia',
	1 => '(GMT +1:00) Amsterdam, Berlin, Brussels, Madrid, Paris, Rome',
	2 => '(GMT +2:00) Cairo, Helsinki, Kaliningrad, South Africa, Warsaw',
	3 => '(GMT +3:00) Baghdad, Riyadh, Moscow, Nairobi',
	3.5 => '(GMT +3:30) Tehran',
	4 => '(GMT +4:00) Abu Dhabi, Baku, Muscat, Tbilisi',
	4.5 => '(GMT +4:30) Kabul',
	5 => '(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent',
	5.5 => '(GMT +5:30) Bombay, Calcutta, Madras, New Delhi',
	5.75 => '(GMT +5:45) Kathmandu',
	6 => '(GMT +6:00) Almaty, Colombo, Dhaka, Novosibirsk, Sri Jayawardenepura',
	6.5 => '(GMT +6:30) Rangoon',
	7 => '(GMT +7:00) Bangkok, Hanoi, Jakarta, Krasnoyarsk',
	8 => '(GMT +8:00) Beijing, Hong Kong, Perth, Singapore, Taipei',
	9 => '(GMT +9:00) Osaka, Sapporo, Seoul, Tokyo, Yakutsk',
	9.5 => '(GMT +9:30) Adelaide, Darwin',
	10 => '(GMT +10:00) Australian Eastern Standard Time, Guam, Vladivostok',
	11 => '(GMT +11:00) Magadan, New Caledonia, Solomon Islands',
	12 => '(GMT +12:00) Auckland, Fiji, Kamchatka, Marshall Island, Wellington',
	13 => "(GMT +13:00) Nuku'alof"
);

foreach($_TZ as $key => $val){
	$offset = ($key) * 3600;
	$mainb .= gmdate('M jS Y, H:i', time()+$offset). ' — '.$val.'<br />';
}
?>

 

This outputs:

May 3, 04:47 am — (GMT -12:00) Eniwetok, Kwajalein
May 3, 05:47 am — (GMT -11:00) Midway Island, Samoa
May 3, 06:47 am — (GMT -10:00) Hawaii
May 3, 07:47 am — (GMT -9:00) Alaska
May 3, 08:47 am — (GMT -8:00) Pacific Time (US & Canada), Tijuana
May 3, 09:47 am — (GMT -7:00) Mountain Time (US & Canada), Arizona
May 3, 10:47 am — (GMT -6:00) Central Time (US & Canada), Mexico City, Central America
May 3, 11:47 am — (GMT -5:00) Eastern Time (US & Canada), Bogota, Lima, Quito
May 3, 12:47 pm — (GMT -4:00) Atlantic Time (Canada), Caracas, La Paz, Santiago
May 3, 01:47 pm — (GMT -3:00) Brasilia, Buenos Aires, Georgetown, Greenland
May 3, 02:47 pm — (GMT -2:00) Mid-Atlantic, Ascension Islands, St. Helena
May 3, 03:47 pm — (GMT -1:00) Azores, Cape Verde Islands
May 3, 04:47 pm — (GMT) Casablanca, Dublin, Edinburgh, Lisbon, London, Monrovia
May 3, 05:47 pm — (GMT +1:00) Amsterdam, Berlin, Brussels, Madrid, Paris, Rome
May 3, 06:47 pm — (GMT +2:00) Cairo, Helsinki, Kaliningrad, South Africa, Warsaw
May 3, 07:47 pm — (GMT +3:30) Tehran
May 3, 08:47 pm — (GMT +4:30) Kabul
May 3, 09:47 pm — (GMT +5:45) Kathmandu
May 3, 10:47 pm — (GMT +6:30) Rangoon
May 3, 11:47 pm — (GMT +7:00) Bangkok, Hanoi, Jakarta, Krasnoyarsk
May 4, 12:47 am — (GMT +8:00) Beijing, Hong Kong, Perth, Singapore, Taipei
May 4, 01:47 am — (GMT +9:30) Adelaide, Darwin
May 4, 02:47 am — (GMT +10:00) Australian Eastern Standard Time, Guam, Vladivostok
May 4, 03:47 am — (GMT +11:00) Magadan, New Caledonia, Solomon Islands
May 4, 04:47 am — (GMT +12:00) Auckland, Fiji, Kamchatka, Marshall Island, Wellington
May 4, 05:47 am — (GMT +13:00) Nuku'alof

Link to comment
https://forums.phpfreaks.com/topic/156678-solved-timezone-issues/
Share on other sites

I did notice that, actually. I'm not sure why it's not even calculating correctly. I tried using the same formula, manually, like so:

 

<?php
$offset = 5.75 * 3600;
echo gmdate('M j, h:i a', time()+$t);
?>

 

This outputted 11:00 instead of 11:15. So it appears to work; Just not inside the foreach loop.

When I include it in this code:

<?php 
echo date('M. jS Y, H:i I', time());
?>

It returns May. 3rd 2009, 12:08 1

 

When I include it in this code:

<?php //The $_TZ Array can be found in my first post.
foreach($_TZ as $key => $val){
	settype($key, 'float');
	$offset = $key * 3600;
	$mainb .= gmdate('M j, h:i a I', time()+$offset). ' — '.$val.'<br />';
}
?>

It returns something similar to May. 3rd 2009, 12:08 0

 

So, with this, I believe it does have something to do with Daylight-Savings Time. How to 'fix' this, however, I remain uncertain.

Edit: What Daniel0 meant is if this is for client use as well, then ask. Otherwise, date will follow the default timezone on your server.

 

You already solved it. Use date! Think man.

 

<?php
$_TZ = array(
      -12 => '(GMT -12:00) Eniwetok, Kwajalein',
      -11 => '(GMT -11:00) Midway Island, Samoa',
      -10 => '(GMT -10:00) Hawaii',
      -9 => '(GMT -9:00) Alaska',
      -8 => '(GMT -8:00) Pacific Time (US & Canada), Tijuana',
      -7 => '(GMT -7:00) Mountain Time (US & Canada), Arizona',
      -6 => '(GMT -6:00) Central Time (US & Canada), Mexico City, Central America',
      -5 => '(GMT -5:00) Eastern Time (US & Canada), Bogota, Lima, Quito',
      -4 => '(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz, Santiago',
      -3.5 => '(GMT -3:30) Newfoundland',
      -3 => '(GMT -3:00) Brasilia, Buenos Aires, Georgetown, Greenland',
      -2 => '(GMT -2:00) Mid-Atlantic, Ascension Islands, St. Helena',
      -1 => '(GMT -1:00) Azores, Cape Verde Islands',
      0 => '(GMT) Casablanca, Dublin, Edinburgh, Lisbon, London, Monrovia',
      1 => '(GMT +1:00) Amsterdam, Berlin, Brussels, Madrid, Paris, Rome',
      2 => '(GMT +2:00) Cairo, Helsinki, Kaliningrad, South Africa, Warsaw',
      3 => '(GMT +3:00) Baghdad, Riyadh, Moscow, Nairobi',
      3.5 => '(GMT +3:30) Tehran',
      4 => '(GMT +4:00) Abu Dhabi, Baku, Muscat, Tbilisi',
      4.5 => '(GMT +4:30) Kabul',
      5 => '(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent',
      5.5 => '(GMT +5:30) Bombay, Calcutta, Madras, New Delhi',
      5.75 => '(GMT +5:45) Kathmandu',
      6 => '(GMT +6:00) Almaty, Colombo, Dhaka, Novosibirsk, Sri Jayawardenepura',
      6.5 => '(GMT +6:30) Rangoon',
      7 => '(GMT +7:00) Bangkok, Hanoi, Jakarta, Krasnoyarsk',
      8 => '(GMT +8:00) Beijing, Hong Kong, Perth, Singapore, Taipei',
      9 => '(GMT +9:00) Osaka, Sapporo, Seoul, Tokyo, Yakutsk',
      9.5 => '(GMT +9:30) Adelaide, Darwin',
      10 => '(GMT +10:00) Australian Eastern Standard Time, Guam, Vladivostok',
      11 => '(GMT +11:00) Magadan, New Caledonia, Solomon Islands',
      12 => '(GMT +12:00) Auckland, Fiji, Kamchatka, Marshall Island, Wellington',
      13 => "(GMT +13:00) Nuku'alof"
   );
   $is_dst = date('I');
   foreach($_TZ as $key => $val){
      $offset = ($key+$is_dst) * 3600;
      $mainb .= gmdate('M jS Y, H:i', time()+$offset). ' — '.$val.'<br />';
   }

Ask the user whether DST applies or detect it automatically based on the rules for that (they differ based on where in the world you are, and some countries don't use it).

 

Yes, I could do it that way (ask the user). In fact, it seems to be the only 'sensible' way. Detecting it automatically seems like it'd be exponentially more work than it might be worth. Thanks Ken2k7 and Daniel0 for your help.

 

Edit: Ken2k7:

I had tried that way and it worked except that it affected all the other timezones, as well. So GMT would be shifted ahead 1-hour, making (at the time of this writing) 7:44-pm instead of it's correct date, 6:44-pm. Anyways, I'll just simply ask the user (I've spent enough time doing this, already). So until, next time; thanks!

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.