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.

Ramakrishnan
  • 5,254
  • 9
  • 34
  • 41
  • 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? – Quentin May 21 '10 at 09:50
  • 3
    `locale` != `location` I beleive you mean `location`, `locale` is a setting for country and language of a system – jigfox May 21 '10 at 09:52

3 Answers3

54

Using

setlocale  (LC_ALL,"0");

to get in the result the actual locale.

sth
  • 222,467
  • 53
  • 283
  • 367
Pol
  • 541
  • 4
  • 3
  • Thanks much!It was indeed in the PHP documentation, but somehow so much right in front of my nose that I had missed it :) – Eric Redon Jan 27 '11 at 14:11
  • This should be the answer of the above question. Thanks a lot!! – Oliver M Grech Aug 26 '13 at 14:00
  • 9
    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! – John Slegers Aug 10 '14 at 20:08
  • 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. – Armin Dec 18 '14 at 10:01
  • my setlocale(LC_ALL,"0") returns "C"... why? – Tobia Jan 16 '15 at 12:27
  • 2
    @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: http://unix.stackexchange.com/questions/87745/what-does-lc-all-c-do – algorhythm Feb 09 '15 at 13:36
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');
}
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
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.

Community
  • 1
  • 1
bobince
  • 528,062
  • 107
  • 651
  • 834
  • 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 May 21 '10 at 10:29
  • 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 May 21 '10 at 11:02
  • +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 May 21 '10 at 11:17
  • +1 for UTC timestamps, as they are easy to sort [alpha]numerically, rather than using complex date arithmetic. – Patanjali Feb 02 '16 at 06:35