tags:

views:

485

answers:

3

How to get Locale using Php,I want to get the time zone based on the location, is there any possibility to get in php.

I need to convert it to GMT and save Database.Again i need to Pull it back to UI with same time same time zone.

A: 

You can use:

date_default_timezone_set('Europe/London');
date_default_timezone_get; // Europe/London

or

if (ini_get('date.timezone')) {
    echo 'date.timezone: ' . ini_get('date.timezone');
}
Sarfraz
Are those double single quotes there for a reason, or is it a typo?
Svish
can i use this to get locale date_default_timezone_get()
Ramakrishnan
@Svish: That was a typo, rightly spotted. Thanks
Sarfraz
@Ramakrishnan: See my updated answer please.
Sarfraz
thanks for your update
Ramakrishnan
+2  A: 

You can't get the client's locale or timezone from the server side, and you can only make a guess at it from the client side, using JavaScript.

(You can get more locale information if the browser supports Java, but that's a bit of a pain and everyone hates applets.)

To handle timezones in a web application, store all your times as UTC timestamps, and convert them to text for on-page formatting using a user-chosen timezone. You can use methods like in this question to guess at the timezone, but since this is not reliable you should also allow the user to explicitly choose timezone.

bobince
we are working on php website that allow the user to configure the schdule time of the message(SMS).so we need allow the end user set time in various zone.
Ramakrishnan
Yes... the user will have to specify the `timezone_identifier` they are using manually, to pass to `date_default_timezone_set` as described by Sarfraz. See http://www.php.net/manual/en/timezones.php for the list of identifiers; you could put them in a drop-down box or other UI. You can use JavaScript to pick a best-guess timezone for the default value of the drop-down, as in the question I linked, or simply default to UTC. Once the user has input a time and timezone, you should convert it to UTC for database storage.
bobince
+1 for storing UTC timestamps. You also might want to check the phpBB source (or a similar PHP app), they have the user select their timezone upon registering, I think that is exactly what you're looking for.
wimvds
A: 

Using

setlocale  (LC_ALL,"0");

to get in the result the actual locale.

Pol