16

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.

2
  • 1
    The locale of the machine PHP is running on or the locale of the user making the HTTP request that is being processes by PHP? Commented May 21, 2010 at 9:50
  • 3
    locale != location I beleive you mean location, locale is a setting for country and language of a system Commented May 21, 2010 at 9:52

3 Answers 3

54

Using

setlocale  (LC_ALL,"0");

to get in the result the actual locale.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks much!It was indeed in the PHP documentation, but somehow so much right in front of my nose that I had missed it :)
This should be the answer of the above question. Thanks a lot!!
I can't get this to work so far on my machine (which is running PHP 5.4). Whatever local I set, "setlocale(LC_ALL,'0')" returns a single letter string with value "C" every single time!
I've got also this behaviour. But not always. Sometimes I get this string LC_CTYPE=de_DE.UTF8;LC_NUMERIC=C;LC_TIME=de_DE.UTF8;LC_COLLATE=de_DE.UTF8;LC_MONETARY=de_DE.UTF8;LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C and sometimes just C. Strange bug.
@JohnSlegers, @Armin, @Tobia: C is the default setting, if no locale was ever set before. It means nothing. Try money_format('%i', 123.45); while you got this C. There will be no currency => 123.45. But set it to en_EN and you will get USD 123.45. You can also set a local and after it set it back to the state before with following call setlocale(LC_ALL, 'C')... See also here: unix.stackexchange.com/questions/87745/what-does-lc-all-c-do
|
1

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.

4 Comments

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.
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 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.
+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.
+1 for UTC timestamps, as they are easy to sort [alpha]numerically, rather than using complex date arithmetic.
1

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');
}

2 Comments

Are those double single quotes there for a reason, or is it a typo?
can i use this to get locale date_default_timezone_get()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.