Using the javascript date/time function is easy because its a client-side language so it displays your system time. But its not the same case with PHP. Since its a server-side language it displays the server’s time which can be irritating because you might have your server located in another country and you might live somewhere else. But changing the timezone is just a few clicks away and whats more, you can even set different timezones for different pages/directories.
Method 1:- Edit the php.ini file
Before editing the php.ini file please take a backup of it. Open the php.ini file using a text editor and navigate to the following line.
date.timezone =
After the equal to (=) symbol enter the desired timezone. See list of PHP timezones for a complete list of supported timezones. For example
date.timezone = America/Mexico_City
save the php.ini file and restart your web server. Create a PHP file and enter the following code
<?php print date("d/m/yy")."<br>".date("H:i:s"); ?>
save the file and access it via your web browser you should see the date and time of the timezone you entered. This change is done system wide so all web pages will reflect this change.
Method 2:- Create/Edit the .htaccess file
This method is for people using shared web hosting services. Others can also use this method for setting different timezones for different directories. Example, for the main directory you may have the Mexico timezone and for the directory /images you can have the Australian timezone. Edit the .htaccess file, if its not there create a file named .htaccess inside the www or htdocs or public_html folder. Add the following line to it
php_value date.timezone [timezone name]
example,
php_value date.timezone America/Mexico_City
save the .htaccess file and do as mentioned in method 1 to test the configuration.
Method 3:- Use the ini_set() function
The ini_set() is a very useful function which allows an user to set specific options for a single page/file. This method can be used be anyone who want to apply different timezone settings to each of the PHP script.
Syntax:- ini_set("date.timezone","[timezone name]");
Example:- ini_set("date.timezone","America/Mexico_City");
Add this function to the beginning of every PHP script and you’re done. So you can have each script show a different time/date associated with a different timezone.
megadr01d says
Old post but method 3 can also be
kthxbye