I want a php date function that adapts to user region an timezones. So if a user is in australia it will show the relevant date, and if the user is in uk it will show his relevant date.
2 Answers
You can set the default timezone for the user using date_default_timezone_set
. Assuming your server's time is correct, this will offset all of the dates correctly.
<?php
date_default_timezone_set('Europe/London');
echo date('d/m/Y H:i:s');
echo "\r\n";
date_default_timezone_set('Europe/Stockholm');
echo date('d/m/Y H:i:s');
echo "\r\n";
Outputs:
Rudis-Mac-Pro:~ rudi$ php tmp.php
03/05/2011 18:15:49
03/05/2011 19:15:49
You could store this setting on a per-user basis and set it upon login (and of course on each time the session reloads).
If you want to do this automagically based on the user's location, your best bet would probably be to use a GeoLocation service (there's a free IP->Location database out there which you could easily integrate). Of course, this isn't 100% reliable, and nothing you do automatically will never be very accurate. You'd be better off asking them to register and define their location, that way it can't be wrong!

- 21,350
- 5
- 71
- 97
The strftime()
function will take the current locale into account when formatting a date. It is similar to date()
except it will print words like the day of the week or the month in the language of the given locale, instead of in English. Use the setlocale()
function to set the locale before calling strftime().
setlocale(LC_TIME, "fr");
echo "Today's day in French: " . strftime("%A");

- 34,873
- 17
- 75
- 109