If you notice the incorrect time in your PHP scripts, the likely culprit is that the hosting server is in a different timezone.
For example, when you’re an InMotion web hosting customer, you can select from data centers in Washington, D.C., or California, which means the server’s timezone will be either Eastern Standard Time (EST) or Pacific Standard Time (PST).
If you want to change that, it’s a quick fix. Simply insert a line of code into your php.ini file and the location you set in the code will determine the time to display in your PHP scripts. Keep reading to learn how.
- Open your php.ini file with the File Manager in cPanel .
- Add the following line of code to top of your php.ini file: date.timezone = "US/Central".
- Replace US/Central with the timezone from here that corresponds to the time you want to display.
- Once you have entered the desired timezone, click Save Changes to save the file.
- Now, you can check your
phpinfo.php page to verify the change took place.
Congratulations! You are now able to set the timezone in PHP. If you wish to see more guides with tips and information for your website issues, please see our InMotion Hosting Support Center!
Sell hosting using your own brand with a powerful Reseller Hosting plan, now with more resources and faster storage using SSDs at no extra cost.
Arnel Custodio Content Writer I
As a writer for InMotion Hosting, Arnel has always aimed to share helpful information and provide knowledge that will help solve problems and aid in achieving goals. He's also been active with WordPress local community groups and events since 2004.
More Articles by Arnel
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
date_default_timezone_set — Sets the default timezone used by all date/time functions in a script
Description
date_default_timezone_set(string $timezoneId): bool
Instead of using this function to set the default timezone in your script, you can also use the INI setting date.timezone to set the default timezone.
Parameters
timezoneIdThe timezone identifier, like UTC, Africa/Lagos, Asia/Hong_Kong, or Europe/Lisbon. The list of valid identifiers is available in the List of Supported Timezones.
Return Values
This function returns false if the timezoneId isn't valid, or true otherwise.
Examples
Example #1 Getting the default timezone
<?php
date_default_timezone_set('America/Los_Angeles');$script_tz = date_default_timezone_get();
if (
strcmp($script_tz, ini_get('date.timezone'))){echo 'Script timezone differs from ini-set timezone.';
} else {
echo 'Script timezone and ini-set timezone match.';
}
?>
See Also
- date_default_timezone_get() - Gets the default timezone used by all date/time functions in a script
- List of Supported Timezones
Pierre Gourlaouen ¶
10 years ago
A simple method for conversation between two time zone.
<?php
$date = new DateTime("2012-07-05 16:43:21", new DateTimeZone('Europe/Paris'));
date_default_timezone_set('America/New_York');
echo
date("Y-m-d h:iA", $date->format('U')); // 2012-07-05 10:43AM?>