When you try to display time, PHP is not displaying your correct local time. It happens because the web server and your location are different. The time that PHP displays is the default local time of the web server. Now, you want a solution which will display your local time in all the scripts by default.
In the php.ini file, a default timezone is set and the time you’re seeing are coming from this timezone. As the current time you see is not your local time, you need to reset the default timezone on the php.ini file according to yours. For this follow the steps below-
Step 1: Locate the php.ini file
- If you’re using cPanel, find the php.ini file in – /home/username/php.ini
- Here, username is your cPanel username.
- If you’re using Plesk for VPS, find the php.ini file in – /var/www/vhost/$domain/etc/php.ini
- Here, $Domain is your domain name.
- If you’re developing php locally in XAMPP server, the php.ini file is in – /xampp/php/
- If you don’t have access in the web server, you can ask server guys for it.
Step 2: Find the default timezone setting
In the php.ini file, search with “timezone” string. You’ll see something like the following image-
In the above image, the default timezone of the web server is “Europe/Berlin”(See the red circle).
Step 3: Replace the default timezone with yours
Now, suppose you live in Rome. To display local Rome time in all your scripts, change the above “Europe/Berlin” to “Europe/Rome” and save the page. See the following picture-
Step 4: Restart your local server
To see the effect on your local server, you may need to restart your web server.
Step 5: Test the change
To test whether the change have taken affect, write the following line in a php script and run-
<?php echo phpinfo(); ?>
It will show PHP’s configuration information. In this page search with “timezone”. If everything is okay, you’ll see the new timezone like the following image-