PHP Shows the Wrong Date or Time: Set the Time Zone Correctly
Dates are hours off. Set the time zone in php.ini or in code and store times in UTC.
Reference guide. This guide is written from established practice and the official documentation of the tools involved. We have not yet tested every step on a live demo site. Back up your site before you change anything.
Set it
In code, near the start of your script:
date_default_timezone_set('Asia/Kolkata');
Or set date.timezone in your PHP settings. See our php.ini guide.
Good practice
- Store times in UTC in the database, and convert to the visitor's time zone when displaying.
- Use the
DateTimeclasses rather than string maths. - Remember daylight saving, which varies by region.
WordPress
Use its time zone setting. See our WordPress time zone guide.
Server time
If the server clock itself is wrong, ask your host.
Related guides
- YouTube Video Embed Not Working or Showing an Error on Your Website
- WordPress XML Sitemap Gives 404 or Is Not Generating
- Changed the WordPress Address (URL) and Now the Site or Login Is Broken
- wp-config.php Explained: The Settings That Matter and How to Edit Them Safely
Did this solve it? If not, email hello@sitefixlab.org with the exact error text and we will look into it. Try the problem finder.