How to Update PHP Timezone

In this tutorial will show how to rapidly check all PHP versions timezone and also update it to a custom value.

To see the current timezone in cPanel for all PHP versions

for phpver in $(ls -1 /opt/cpanel/ |grep ea-php | sed 's/ea-php//g') ; do echo "PHP $phpver" ; /opt/cpanel/ea-php$phpver/root/usr/bin/php -i |grep -Ei 'timezone' && echo "" ; done

Example:

[root@cpanel ~]# for phpver in $(ls -1 /opt/cpanel/ |grep ea-php | sed 's/ea-php//g') ; do echo ""; echo "PHP $phpver" ; /opt/cpanel/ea-php$phpver/root/usr/bin/php -i |grep -Ei 'date.timezone' && echo "" ; done

PHP 70
date.timezone => UTC => UTC


PHP 71
date.timezone => UTC => UTC


PHP 72
date.timezone => UTC => UTC


PHP 73
date.timezone => UTC => UTC


PHP 74
date.timezone => UTC => UTC

[root@cpanel ~]#

This is the all in one command set to do this for cPanel. Command uses “America/Phoenix” as the timezone in the example. Change this to your desired timezone database name from here.

for phpver in $(ls -1 /opt/cpanel/ |grep ea-php | sed 's/ea-php//g') ; do echo "PHP $phpver" ; sed -i 's|^date.timezone.*|date.timezone = "America/Phoenix"|g' /opt/cpanel/ea-php$phpver/root/etc/php.ini && /opt/cpanel/ea-php$phpver/root/usr/bin/php -i |grep -Ei 'date.timezone' && echo "" ; done; service httpd restart; killall lsphp;

Example:

[root@cpanel ~]# for phpver in $(ls -1 /opt/cpanel/ |grep ea-php | sed 's/ea-php//g') ; do echo ""; echo "PHP $phpver" ; sed -i 's|^date.timezone.*|date.timezone = "America/Phoenix"|g' /opt/cpanel/ea-php$phpver/root/etc/php.ini && /opt/cpanel/ea-php$phpver/root/usr/bin/php -i |grep -Ei 'date.timezone' && echo "" ; done; service httpd restart; killall lsphp;

PHP 70
date.timezone => America/Phoenix => America/Phoenix


PHP 71
date.timezone => America/Phoenix => America/Phoenix


PHP 72
date.timezone => America/Phoenix => America/Phoenix


PHP 73
date.timezone => America/Phoenix => America/Phoenix


PHP 74
date.timezone => America/Phoenix => America/Phoenix

Redirecting to /bin/systemctl restart httpd.service
lsphp: no process found
[root@cpanel ~]#

For Cyberpanel this can be done the same way to check the current value.

For Centos:

for version in $(ls /usr/local/lsws|grep lsphp); do echo ""; echo "LSPHP $version"; /usr/local/lsws/${version}/bin/php -i |grep -Ei 'date.timezone' && echo "" ; done

For Ubuntu:

for phpver in $(ls -1 /usr/local/lsws/ |grep lsphp | sed 's/lsphp//g') ; do echo ""; echo "LSPHP $phpver" ; /usr/local/lsws/lsphp${phpver}/bin/php -i |grep -Ei 'date.timezone' && echo "" ; done

This is the all in one command set to do this for Cyberpanel. Command uses “America/Phoenix” as the timezone in the example. Change this to your desired timezone database name from here.

Centos:

for version in $(ls /usr/local/lsws|grep lsphp); do echo ""; echo "PHP $version"; sed -i 's|^date.timezone.*|date.timezone = "America/Phoenix"|g' /usr/local/lsws/${version}/etc/php.ini; /usr/local/lsws/${version}/bin/php -i |grep -Ei 'date.timezone' && echo "" ; done

Ubuntu:

for phpver in $(ls -1 /usr/local/lsws/ |grep lsphp | sed 's/lsphp//g') ; do echo ""; echo "LSPHP $phpver" ; sed -i -e 's|^date.timezone.*|date.timezone = "America/Phoenix"|g' /usr/local/lsws/lsphp${phpver}/etc/php/$(echo $phpver | sed 's/^\(.\{1\}\)/\1./')/litespeed/php.ini; /usr/local/lsws/lsphp${phpver}/bin/php -i |grep -Ei 'date.timezone' && echo "" ; done; service lsws restart; killall lsphp;

Special Note if the date.timezone value is unset then the below custom sed command can be adjusted to uncomment and change that commented line into a valid one.

If no value set like the below.

root@server:~# for phpver in $(ls -1 /usr/local/lsws/ |grep lsphp | sed 's/lsphp//g') ; do echo ""; echo "LSPHP $phpver" ; /usr/local/lsws/lsphp$phpver/bin/php -i |grep -Ei 'date.timezone' && echo "" ; done

LSPHP 70
date.timezone => no value => no value


LSPHP 71
date.timezone => no value => no value


LSPHP 72
date.timezone => no value => no value


LSPHP 73
date.timezone => no value => no value


LSPHP 74
date.timezone => no value => no value

root@server:~#

Centos:

for version in $(ls /usr/local/lsws|grep lsphp); do echo ""; echo "PHP $version"; sed -i 's|^;date.timezone.*|date.timezone = "America/Phoenix"|g' /usr/local/lsws/${version}/etc/php.ini; /usr/local/lsws/${version}/bin/php -i |grep -Ei 'date.timezone' && echo "" ; done

Ubuntu:

for phpver in $(ls -1 /usr/local/lsws/ |grep lsphp | sed 's/lsphp//g') ; do echo ""; echo "LSPHP $phpver" ; sed -i -e 's|^;date.timezone.*|date.timezone = "America/Phoenix"|g' /usr/local/lsws/lsphp${phpver}/etc/php/$(echo $phpver | sed 's/^\(.\{1\}\)/\1./')/litespeed/php.ini; /usr/local/lsws/lsphp${phpver}/bin/php -i |grep -Ei 'date.timezone' && echo "" ; done; service lsws restart; killall lsphp;

Hopefully you enjoyed this post and found how to rapidly change all the PHP version timezone to your desired ones.

For those curious the “killall lsphp” is there for those using LSAPI/LSPHP/Litespeed which has detached processes that don’t get restarted unless killed. Without that line it would look like the changes were not applied so this is a sanity timesaver to prevent people unfamiliar with that from wasting time wondering why the PHP changes don’t look like there working. If you like the post feel free to bookmark share or repost.