How would I take a stored date, like 2011-01-30 18:23:49, and adjust it to any chosen timezone? Is there a simple way such as simply defining the time zone by abbreviation or adding/subtracting x amount of hours?
Basically I want users to be able to choose their time zone and this default date be adjusted to fit theirs.
How would I take a stored date, like 2011-01-30 18:23:49, and adjust it to
Share
Have the user choose their time zone
Use that zone name or offset with
date_default_timezone_setto set the default time zone used in date functions throughout the rest of script execution.Use
date('Z')to get that time zone’s offset from GMT in secondsConvert your stored date to a timestamp with
strtotime— UNIX timestamps are always GMT, so you now have the time in GMT.Add the offset from step 3 to convert that time to the user’s time zone.
Use
dateagain to format the timestamp as a string in the desired display format.Example: