+ 4
How do I work with different time zones when storing and displaying dates?
Posts are stored with my local time in format DD-MM-YYY. I fetch these dates and convert them to text like "3 months ago" etc... But before that I would need to convert that date to the client time zone.
3 odpowiedzi
+ 3
Storing
Run this at start of app:
date_default_timezone_set('UTC') ;
Then store date
E.g
$date = new DateTime() ;
Access Object
$date->format();
Then store this into MySql datetime type column.
Displaying:
Get Time Info with JS and store as Cookie.
Run Mysql select to output datetime.
$Date = new DateTime($row['time']); to instantiate a DateTime object with the stored UTC time.
$Date->setTimezone(new DateTimeZone($userTimezone)); to adjust the UTC time to the user's timezone.
Display using $Date->format();
Hope it Helps 👍
+ 2
convert any time to Unix timestamp and display the same when you need to display
+ 1
All timezones are in regard to gmt.
I'm at gmt+3.
and you are at... ?