PHP MYSQL: list of Timezones, Set Timezone


Recently I needed to check the time zone in one of my applications, because it is accessed from multiple locations in United States and soon will be accessed from other countries.

The first step was to get an organized list of timezone in PHP. I found an interesting article that shows all Time Zones in an organized way.

Step 1: Create a table in your database:
CREATE TABLE IF NOT EXISTS `timezone` (
`timezid` int(11) NOT NULL auto_increment,
`tz` varchar(255) NOT NULL,
`gmt` text NOT NULL,
PRIMARY KEY (`timezid`)
) ENGINE=MyISAM;

Step 2: Insert the records:
INSERT INTO timezone (timezid, tz, gmt) VALUES
(1, 'Pacific/Kwajalein', '(GMT -12:00) Eniwetok, Kwajalein'),
(2, 'Pacific/Samoa', '(GMT -11:00) Midway Island, Samoa'),
(3, 'Pacific/Honolulu', '(GMT -10:00) Hawaii'),
(4, 'America/Anchorage', '(GMT -9:00) Alaska'),
(5, 'America/Los_Angeles', '(GMT -8:00) Pacific Time (US & Canada) Los Angeles, Seattle'),
(6, 'America/Denver', '(GMT -7:00) Mountain Time (US & Canada) Denver'),
(7, 'America/Chicago', '(GMT -6:00) Central Time (US & Canada), Chicago, Mexico City'),
(8, 'America/New_York', '(GMT -5:00) Eastern Time (US & Canada), New York, Bogota, Lima'),
(9, 'Atlantic/Bermuda', '(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz'),
(10, 'Canada/Newfoundland', '(GMT -3:30) Newfoundland'),
(11, 'Brazil/East', '(GMT -3:00) Brazil, Buenos Aires, Georgetown'),
(12, 'Atlantic/Azores', '(GMT -2:00) Mid-Atlantic'),
(13, 'Atlantic/Cape_Verde', '(GMT -1:00 hour) Azores, Cape Verde Islands'),
(14, 'Europe/London', '(GMT) Western Europe Time, London, Lisbon, Casablanca'),
(15, 'Europe/Brussels', '(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris'),
(16, 'Europe/Helsinki', '(GMT +2:00) Kaliningrad, South Africa'),
(17, 'Asia/Baghdad', '(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg'),
(18, 'Asia/Tehran', '(GMT +3:30) Tehran'),
(19, 'Asia/Baku', '(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi'),
(20, 'Asia/Kabul', '(GMT +4:30) Kabul'),
(21, 'Asia/Karachi', '(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent'),
(22, 'Asia/Calcutta', '(GMT +5:30) Bombay, Calcutta, Madras, New Delhi'),
(23, 'Asia/Dhaka', '(GMT +6:00) Almaty, Dhaka, Colombo'),
(24, 'Asia/Bangkok', '(GMT +7:00) Bangkok, Hanoi, Jakarta'),
(25, 'Asia/Hong_Kong', '(GMT +8:00) Beijing, Perth, Singapore, Hong Kong'),
(26, 'Asia/Tokyo', '(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk'),
(27, 'Australia/Adelaide', '(GMT +9:30) Adelaide, Darwin'),
(28, 'Pacific/Guam', '(GMT +10:00) Eastern Australia, Guam, Vladivostok'),
(29, 'Asia/Magadan', '(GMT +11:00) Magadan, Solomon Islands, New Caledonia'),
(30, 'Pacific/Fiji', '(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka');

Step 3: Create a php script that fetches the timezone and prints the current time:

<?php
$conn = mysql_connect("HOST" ,"USER" , "PASS");
mysql_select_db("DATABASE");
$query = mysql_query("SELECT * FROM timezone", $conn);

echo "<h1>Time Zone:</h1>";
while($row = mysql_fetch_array($query, MYSQL_BOTH)){
    echo $row["gmt"], "<BR>";
}

date_default_timezone_set("Brazil/East");
echo "<HR/>Current time in Buenos Aires: ", date("H:i:s");

date_default_timezone_set("Pacific/Honolulu");
echo "<BR/>Current time in Hawaii: ", date("H:i:s");
?>

We can set Timezone with 2 methods:

* (PHP 5 >= 5.1.0)

date_default_timezone_set('One/Time_Zone');

* (PHP 5 >= 5.2.0)

$timezone = new DateTimeZone("One/Time_Zone" );
$date = new DateTime();
$date->setTimezone($timezone);

Recent search terms:

  • mysql timezone tutorial
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1874 xIv1VoqvEU5v30Gm9iCZngXgpVd7Sv2g4NOMmUNftQyviPyhRBJmaMkoInCzGAPe 50c8607c20c6e88467ed867e6570e92d29d0d4df&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme

Leave a Reply