Jump to content

date time "always" wrong


StevenOliver

Recommended Posts

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 by StevenOliver
Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.