[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
timezoneId
The 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
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.
Why do I need to set the PHP timezone?
Most commonly used PHP scripts allow you to set the timezone as one of their settings. WordPress, for example, has a drop-down list within the Settings > General menu. However, this isn’t always the case. To ensure that the time on your scripts matches your local time, you can configure a default timezone in your php.ini file.
cPanel MultiPHP
If you’re using EasyApache4 on a cPanel server, log into WHM and visit the ‘MultiPHP INI Editor’ option.
Select ‘Editor Mode’ and then the PHP version you’re using from the dropdown list.
Scroll down to the ‘Module Settings’ section and you’ll see the [Date] option.
Remove any preceding ; before the ‘date.timezone’ setting and then add your locale. There’s a list of available options here.
In our case the line now looks like this:
date.timezone = "Europe/London"
Click to save and you are done. Depending on your webserver configuration, you may need to restart it for the change to take effect.
CentOS 7 without cPanel
When Apache and PHP are installed via yum the php.ini file is found in the /etc/ directory.
Using your favourite editor [I’m a big nano fan] open up /etc/php.ini
[user@server] sudo nano /etc/php.ini
Search for the ‘data.timezone’ directive – ctrl+w on nano
Not just remove the ; and add the locale of your choice [list here] leaving a line looking like this:
date.timezone = "Europe/London"
Save the file then restart apache and you’re done.
The default timezone for Apache/PHP is UTC [Coordinated Universal Time]. Sometimes you may need to change Apache/PHP time zone to sync it with other systems or because your customers are located in a different timezone. In this article, we will look at how to change default timezone in Apache/PHP. You can use these steps to set Apache/PHP timezone as per your requirement.
Here is how to set Apache/PHP timezone. We will look at two ways to set default timezone – using .htaccess in Apache and using php.ini file in PHP.
Change default timezone using .htaccess file
In case you don’t use Apache with PHP, or you don’t have access to php.ini, you can set default time in Apache web server using .htaccess file. Before proceeding further, please make sure that you have enabled mod_rewrite [.htaccess] on your Apache web server.
Open .htaccess file, typically located at /var/www/html/.htaccess
$ sudo vi /var/www/html/.htaccess
Add the following line to set environment variable TZ [for timezone] in Apache web server to Eastern Time zone in US.
SetEnv TZ America/New_York
Save and close the file.
Also read : How to Set Up Apache Virtual Hosts in Apache
In the above line, we specify SetEnv TZ to set timezone environment variable in Apache, followed by ‘America/New York’ to specify Eastern Time. Here is a list of all available timezones in Apache/PHP.
Restart Apache Server to apply changes.
$ sudo service apache2 restart
Also read : How to Install mod_security in CentOS 7
Change default timezone using php.ini
If you don’t have access to .htaccess file or if you use PHP with Apache, then here is how to set default timezone in Apache/PHP.
Open php.ini, typically at /etc/php.ini
$ sudo vi /etc/php.ini
Uncomment/Add the following lines to set default timezone in PHP.
;[Date]
;date.timezone = UTC
Change them to the following, to set default time to Eastern timezone.
[Date]
date.timezone =
America/New_York
Also read : How to Create Custom 404 Page in Apache
Restart Apache Server to apply changes.
$ sudo service apache2 restart
That’s it. Now your default timezone in Apache will be changed. Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!
Related posts:
- About Author