Skip to Content

Correct PHP time in different OS

How to correct PHP time in Ubuntu, Debian, Centos, RHEL?

This article will help the Linux user to correct the PHP time in different OS. Follow below steps to fix the PHP time.

a) Create a PHP page to show up the current time in all OS.

root@server ~]# vim /var/www/html/time.php

<?php
$current_time = date("Y-m-d H:i:s");
echo "Current time: " . $current_time;
?>

b) Check the current time by running the below command or by using web browser (server_ip/time.php)

root@server ~]# php /var/www/html/time.php
Current time: 2024-04-05 06:47:58

1. How to correct PHP time in Ubuntu 18, 20, 22 and Debian 10, 11, 12?

Edit the php.ini configuration file and add correct timezone

root@server ~]# vim /etc/php/8.2/apache2/php.ini

date.timezone = Asia/Kolkata

root@server ~]# vim /etc/php/8.2/cli/php.ini

date.timezone = Asia/Kolkata

root@server ~]# systemctl restart apache2

2. How to correct PHP time in CentOS 6, 7, 8, RHEL 7, 8, 9 , RockyLinux, AlmaLinux?

root@server ~]# vim /etc/php.ini

date.timezone = Asia/Kolkata

root@server ~]# systemctl restart httpd

c) After restarting the web service check the time again from shell and web browser.

root@server ~]# php /var/www/html/time.php
Current time: 2024-04-05 14:18:58