How can I find country name -> GMT date/time to that I can do like following:
Example:
$datetime = new GMT_search['America']; //output: 2010-01-01 00:00:00
$datetime = new GMT_search['India']; //output: 2010-01-01 ??:??:??
$datetime = new GMT_search['China']; //output: 2010-01-01 ??:??:??
I tried gmdate[]
, date_default_timezone_set['Asia/....'];
, and ini_set['date.timezone','China'];
but it’s not exactly helping me to find easily country name to GMT date/time.
Can anyone please kindly show me a PHP example, which really works?
Thank you
Gumbo
627k106 gold badges766 silver badges837 bronze badges
asked Aug 14, 2010 at 13:32
13
You can search the timezones
by country with DateTimeZone::listIdentifiers
.
Example, to get the timezones in Portugal:
print_r[DateTimeZone::listIdentifiers[DateTimeZone::PER_COUNTRY, "PT"]];
gives:
Array [ [0] => Atlantic/Azores [1] => Atlantic/Madeira [2] => Europe/Lisbon ]
You can then do:
$d = new DateTime["now", new DateTimeZone["Atlantic/Azores"]];
echo $d->format[DateTime::W3C]; //2010-08-14T15:22:22+00:00
As has been repeated over and over again in this thread, you can't get one single time zone per country. Countries have several timezones, and you'll notice that even this page doesn't even select one arbitrarily for some countries like the U.S.A.
answered Aug 14, 2010 at 15:28
ArtefactoArtefacto
94.3k16 gold badges194 silver badges221 bronze badges
2
Here you'll find the complete
list of timezones supported by PHP, which are meant to be used with e.g. date_default_timezone_set[]
. Support for countries with multiple timezones is also convenient to look up. Take the example of the American region.
The list is a complete timezones supported by PHP, which are meant to be used with e.g. date_default_timezone_set[].
answered Aug 14, 2010 at 13:38
Jungle HunterJungle Hunter
7,15911 gold badges41 silver badges66 bronze badges
13
You can't do it quite like that, since a lot of countries have multiple timezones. You could store the timezone by the name of a city instead, but I'd use an integer with the timezone offset in seconds.
You can get a list of timezones by continent/city from this function: [www.php.net/manual/en/function.timezone-identifiers-list.php][1]
You can get the respective offset from this function: [www.php.net/manual/en/datetimezone.getoffset.php][2]
When you have a valid offset, you can use gmdate[] instead of date[] to get a date in your format without the timezone/dst adjustment. Just pass the time[] + the ajustment you have stored: [www.php.net/manual/en/function.gmdate.php][3]
answered Aug 14, 2010 at 14:14
geongeon
7,7683 gold badges33 silver badges40 bronze badges
3
[PHP 5 >= 5.1.0, PHP 7, PHP 8]
date_default_timezone_get — Gets the default timezone used by all date/time functions in a script
Description
date_default_timezone_get[]: string
Reading the timezone set using the date_default_timezone_set[] function [if any]
Reading the value of the date.timezone ini option [if set]
If none of the above succeed, date_default_timezone_get[] will return a default timezone of UTC
.
Parameters
This function has no parameters.
Return Values
Returns a string.
Examples
Example #1 Getting the default timezone
The above example will output something similar to:
date_default_timezone_set: Europe/London date.timezone: Europe/London
Example #2 Getting the abbreviation of a timezone
The above example will output:
America/Los_Angeles => America/Los_Angeles => PST
See Also
- date_default_timezone_set[] - Sets the default timezone used by all date/time functions in a script
- List of Supported Timezones
There are no user contributed notes for this page.