StevenOliver Posted May 2, 2020 Share Posted May 2, 2020 (edited) Question: For a website on a shared server (regardless of server location), is there such a thing as PHP code that will consistently produce my own time zone's time without having to adjust every few months? Background: No matter how much I "adjust" the code, time zones, etc., things like daylight saving time, or server location throws it off by 1, 2, sometimes 3 hours. Example: This is my code:$date = new DateTime(); $currentTime = $date->sub(new DateInterval('PT8H'))->format('F j, Y \a\t g:ia'); The Pacific ("Los Angeles") time right now is 7:58pm, but my code says it's 6:58pm. So now I have to go in and correct the "PT8H" portion. I'm wondering if there is some code I can use whereby I can simply "set it and forget it." Thank you! Edited May 2, 2020 by StevenOliver Quote Link to comment Share on other sites More sharing options...
kicken Posted May 2, 2020 Share Posted May 2, 2020 date_default_timezone_set Setup the timezone to the one you want. For example: date_default_timezone_set('America/Los_Angeles') Then just use your date functions normally. Quote Link to comment Share on other sites More sharing options...
StevenOliver Posted May 2, 2020 Author Share Posted May 2, 2020 Kicken, thank you. I added your line of code and removed the "sub(new DateInterval('PT8H'))" portion. It now gives the correct time! 😀 date_default_timezone_set('America/Los_Angeles'); $date = new DateTime(); $currentTime = $date->format('F j, Y \a\t g:ia'); echo $currentTime; I guess that means I never have to monkey with it again, adding and subtracting for daylight time, server location, etc. Thank you again!! Quote Link to comment Share on other sites More sharing options...
Barand Posted May 2, 2020 Share Posted May 2, 2020 The best place to set the default timezone is in your php.ini file. Then you don't have to set the default in every script. Quote Link to comment Share on other sites More sharing options...
maxxd Posted May 2, 2020 Share Posted May 2, 2020 Caveat to keep in mind, though - any time/date data you've inserted into a database is now technically incorrect as it was inserted using the server's timezone. If you have any queries that pull data by date/time, that may become a factor - daylight savings and whatnot can sometimes cause returns to be a full day off, depending on how the query is built and how the data was stored. Of course, that could just be the lingering PTSD from the several months long timezone-based project I just finished at work... Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 3, 2020 Share Posted May 3, 2020 Maxxd is right. You should never do this, but it's a common mistake that is hard to recover from unfortunately. You should always store datetime values as UTC, and this should be the timezone of the server OS, php and mysql. Once you know it's in UTC, you convert the date using a timezone for the purposes of display. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.