views:

90

answers:

4

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

+2  A: 

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")

DrColossos
+1  A: 

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().

Ashish
I have country only, NO CITY database. How can you make Country To::GMT_DateTime?
Stackfan
Should i have to make, country with default city? I dont want to specific city date/time? But again i have problem with Asia or Europe?. ex: $country = array('America'=>'New_York', 'India'=>'Mumbai', 'China' =>'Sangai');
Stackfan
There is the possibility that a country will have more than one timezone. So trying to convert a country name would produce inaccurate results.
cdburgess
You mean you only have the country information but not the city? Or are you looking for [this](http://pecl.php.net/get/timezonedb)? If you only have country information you may store all capitals maybe? And like cdburgess said, what about countries with multiple timezones?
Ashish
How can i put here COUNTRY NAME? array('America'=>'New_York', 'Asia'=>'India', 'Asia'=>'China');???? wrong!! ex: date_default_timezone_set(array.key . '/' . array.value); // wrong again
Stackfan
OK - I understand country like (usa/russia have more, but that is not my problem), I am trying just to do select by COUNTRY NAME. EX: wwp.greenwichmeantime.com/time-zone/all.htm
Stackfan
No, I meant, if you (on your part out of your project constraints) don't have a city you wanna lookup the timezone of, then you store them as per the city that defines the timezone. Like for India you have `Asia/Calcutta`.
Ashish
You can't have it by country using that. I'm suggesting storing the relevant cities for the country.
Ashish
http://countrycode.org/ almost this table in my database, one new field will be coming call TimeZone.My database table fields: ID, Country, CountryCode, TimeZone, Telephone code;;;; Row-1: 1, United States, US, America/New_York, 1;;;; Row2: 2, India, IN, Asia/Mumbai, 91;;;; So a user is controled by COUNTRY CODE OR COUNTRY NAME, automatically that user timezone is 1 timezone, even if you are in calcuta/other city (not my issue). ex: date_default_timezone_set('India'); Thats why, Can you do this by COUNTRY NAME with one default city on it? And is this a GMT time getting returned?
Stackfan
Why do you need this if I may ask? You can [get visitors local time using PHP and JavaScript](http://www.webmasterworld.com/php/3398104.htm) if you would like that or you can get someone to select their timezone from a drop-down like most forums do. But I'm not sure if this is what you are looking for.
Ashish
Call center for A to B and B to C with other D/E/F Time Zone programmers, based on Country (not city).
Stackfan
Don't quite get you. Do the options I suggest in the comment above work for you? I'm sorry to say, there aren't any timezones by country. Because not all countries have *a* (singular) timezone just like you suggested. They can have multiple.
Ashish
Or does [determining timezone by IP](http://derickrethans.nl/detecting-timezone-by-ip.html) work for you?
Ashish
+2  A: 

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

You can get the respective offset from this function: www.php.net/manual/en/datetimezone.getoffset.php

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

Sorry for not posting links, but I don't have enough karma.

geon
I am trying this where you select only COUNTRY NAME. EX: http://wwp.greenwichmeantime.com/time-zone/all.htm
Stackfan
That i dont want. Its about COUNTRY NAME.
Stackfan
@Stackfan: Yes, I chose the "United States of America" link which brought me to .../time-zone/usa and the mentioned page content. So, you gave an example of what you _don't_ want? You also wrote `Yes!! Capital/Down town i am talking. (COUNTRY NAME + CAPITAL) === COUNTRY NAME === ` , which one is it? `country name` or `country name+capital` ?
VolkerK
+2  A: 

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.

Artefacto
Thanks, i was trying to find how Country Name/Land code to reach City or Timezone. ex: print_r(DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, "PT"));
Stackfan
The perfect thing/beauty of your code example is here: 1. I will put land code now to search whatever 2. i will check the array length and pick any one 3. i have my result. Thanks again..
Stackfan