1

I am looking for solution in javascript or PHP which format "date" and "time" based on the visitors location and locale settings.

For example

echo auto_format_date(time());

would output 20-09-2011 for users from states and 20/09/2011 for UK users.


Thanks everyone for answers. I've got now several alternatives in front of me, I'll wait for some up-votes to see which approach you think is preferred

romaninsh
  • 10,606
  • 4
  • 50
  • 70
  • http://stackoverflow.com/questions/863474/automatically-detect-users-current-local-time-with-javascript-or-php – Mob Sep 20 '11 at 09:20
  • Let the user choose what they want - a person's geographic location doesn't necessarily indicate the format that they prefer. – RobG Sep 20 '11 at 09:35
  • Have you ever chosen locale on your mobile phone? User shouldn't bother with those things. – romaninsh Sep 20 '11 at 09:49
  • Think it should be month first in your example for US users, e.g. 09-20-2011 but i agree with RobG that whilst you can default the locale based on whatever info you can find from the browser agent, it's always best to allow the user to have the opportunity to override it in user preferences e.g. i have been in China for 18 months now and sometimes surf directly from the hotel internet connection but also have both UK and German VPN accounts for work. It would be annoying if everytime i went to a site, it changed all the locale settings because it decided i was a Chinese or German user. – Trevor North Oct 17 '11 at 08:33
  • Your locale settings would be the same though. You can change the setting, in your local Control Panel. That is, assuming javascript picks it up correctly. – romaninsh Oct 17 '11 at 09:17

4 Answers4

0

In server side code (PHP, ASP.NET...) you can check Accept-Language HTTP header of request to detect client browser language.

Accept-Language: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4

You can read additional info: http://www.w3.org/International/questions/qa-accept-lang-locales

Andrew D.
  • 8,130
  • 3
  • 21
  • 23
0

You have to get Country from Ip by PHP Geolacator

and than use zned function: date_default_timezone_set('Europe/Berlin');

and when you create date:

$date = new Zend_Date();

echo $date;

You will get what you want...

tasmaniski
  • 4,767
  • 3
  • 33
  • 65
0

I have found the following myself:

http://www.w3schools.com/jsref/jsref_tolocalestring.asp

which might help me format date into user's preferred format once it in the browser. Is this a good solution?

romaninsh
  • 10,606
  • 4
  • 50
  • 70
  • Date.toLocaleString() implemented but does not works as expected in WebKit browsers like Chrome,Safari. – Andrew D. Sep 20 '11 at 10:05
  • I was also googling that. It seems that it's not broken, it is just different across all the browsers. It also looks rather long to be usable. – romaninsh Sep 20 '11 at 10:14
  • 1
    According with my knowledge, in Chrome (russian version) Date.toLocaleString() return string in english. And, Yes, toLocaleString() in IE,Opera,FireFox returns proper string (for locale) , but all has different result. – Andrew D. Sep 20 '11 at 10:20
0

I found another answer, which relies on datejs

https://code.google.com/p/datejs/wiki/FormatSpecifiers

%x     preferred date representation for the current locale 
       without the time        "4/13/2008"
%X     preferred time representation for the current locale
       without the date        "12:53:05"
romaninsh
  • 10,606
  • 4
  • 50
  • 70