When you try to show current date-time or time in your PHP Scripts and get the incorrect value it means your hosting server has a different timezone.
Setting the right default timezone is very important if you want to work with the date and the time of your web application.
In this tutorial, you will learn how to setup default timezone in the php.ini file or PHP script.
Set the PHP Timezone in PHP Script
You can easily set timezone in your PHP script. You just need to add the date_default_timezone_set(“your time zone”) function in your web application config file.
Adding it to the config file will set it for all PHP files.
<?php
date_default_timezone_set("Asia/Calcutta");
?>
Replace ‘Asia/Calcutta’ with your timezone.
If you don’t know your timezone, then check this out.
https://www.php.net/manual/en/timezones.php
Set the PHP Timezone in the php.ini File
Set the PHP Timezone in the php.ini File:
Login to cPanel
Open your php.ini file with the File Manager
Add the following line of code to top of your php.ini file: date.timezone = “Asia/Calcutta”
Replace Asia/Calcutta with the timezone from here that corresponds to the time you want.
Click Save Changes button to save the file.
Now access phpinfo page to see the changes.
Want to know how to get current date and time in PHP. Read this tutorial.
How to Get the Current Date and Time in PHP
So, in this tutorial, you have learned how to change timezone in PHP. If you have any query related to this tutorial, feel free to comment.