How do I work with different time zones when storing and displaying dates? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

18th Aug 2018, 11:40 AM
Toni Isotalo
Toni Isotalo - avatar
3 Answers
+ 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 👍
19th Aug 2018, 8:05 AM
Femi
Femi - avatar
+ 2
convert any time to Unix timestamp and display the same when you need to display
19th Aug 2018, 11:09 AM
Pavan Karumuri
Pavan Karumuri - avatar
+ 1
All timezones are in regard to gmt. I'm at gmt+3. and you are at... ?
18th Aug 2018, 12:10 PM
Amir Galanty
Amir Galanty - avatar