How to set PHP timezone

Tech
No Comments

Why do I need to set the PHP timezone?

Most commonly used PHP scripts allow you to set the timezone as one of their settings.  WordPress, for example, has a drop-down list within the Settings > General menu.  However, this isn’t always the case.  To ensure that the time on your scripts matches your local time, you can configure a default timezone in your php.ini file.

cPanel MultiPHP

If you’re using EasyApache4 on a cPanel server, log into WHM and visit the ‘MultiPHP INI Editor’ option.

Select ‘Editor Mode’ and then the PHP version you’re using from the dropdown list.

Scroll down to the ‘Module Settings’ section and you’ll see the [Date] option.

Remove any preceding ; before the ‘date.timezone’ setting and then add your locale.  There’s a list of available options here.

In our case the line now looks like this:

date.timezone = "Europe/London"

Click to save and you are done.  Depending on your webserver configuration, you may need to restart it for the change to take effect.

CentOS 7 without cPanel

When Apache and PHP are installed via yum the php.ini file is found in the /etc/ directory.

Using your favourite editor (I’m a big nano fan) open up /etc/php.ini

[user@server] sudo nano /etc/php.ini

Search for the ‘data.timezone’ directive – ctrl+w on nano

Not just remove the ; and add the locale of your choice (list here) leaving a line looking like this:

date.timezone = "Europe/London"

Save the file then restart apache and you’re done.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu