Php timezones and list visited

1.6k views Asked by At

First, i want to set default timezone for my website for GMT, i found this

htaccess:

php_value date.timezone "America/Chicago"

SetEnv TZ GMT

SetEnv TZ Europe/Amsterdam

(i know America/Chicago is not GMT ;) ) how should correct option looks like?

second, i want to save user timezone when he register and also allow him to change timezone in his profile settings

last, how to use time zone chosen by user? i save all times on my website to database as unix timestamp (time())

1

There are 1 answers

3
vascowhite On BEST ANSWER

You should set date.timezone to UTC in php.ini if you have access to it, if not then do it in .htaccess like this:-

php_value date.timezone UTC

Your application will now default to using UTC as the timezone unless you specify otherwise. You can store user timezones using the strings in this list. Just allow your users to choose the correct one from a drop down.

Your application should store all date/times as UTC using either a unix or mysql timestamp. My personal preference is for unix timestamps.

When presenting dates to the user make use of the DateTime classes. For example:-

$timeZoneString = getTimeZoneFromDB();
$tz = new \DateTimeZone($timeZoneString);
$date = (new \DateTime())->setTimeZone($tz);
echo $date->format('Y-m-d H:i:s);

You should not use date_default_timezone_set() to modify timezones on the fly as this can have unpredictable side affects in your app. If that particular function is used at all, it should be only once in your app. If it appears more than that, you should consider it a code smell.