How to find GMT date/time by country name?

28,299

Solution 1

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.

Solution 2

Not sure if this is what you are looking for but PEAR has a "Date" package available that has a nice example that seems to do what you are asking for.

http://pear.php.net/manual/en/package.datetime.date.examples.php (scroll down to "Converting timezones")

Solution 3

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.

<?php
date_default_timezone_set('America/New_York');
echo date('D,F j, Y, h:i:s A');
?>

The list is a complete timezones supported by PHP, which are meant to be used with e.g. date_default_timezone_set().

Solution 4

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]

Share:
28,299
Admin
Author by

Admin

Updated on November 14, 2020

Comments

  • Admin
    Admin over 3 years

    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