Php set timezone linux

How to set 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.

Источник

Howto Setup Timezone in php.ini or PHP Script on Linux Systems

undefined

Setup the default timezone is very necessary for proper data processing send from various countries. This article will help you to know how can we setup a default timezone in PHP configuration file (php.ini) or inside any php script for temporary use.

Part 1: Setup Timezone in php.ini

To setup default timezone for your php environment, follow below 4 simple steps.

1. Location of php.ini – Find out your correct php.ini configuration file. Following is some default php.ini locations

2. Choose correct timezone – What timezone you need to configure in your php.ini file. Use below link to find available timezones for php.

3. Update timezone in php.ini – Finally edit your php.ini configuration file and update proper timezone value in date.timezone setting tag. For example you want to configure ‘America/New_York’ as your default timezone.

date.timezone = "Africa/Johannesburg"

4. Restart Apache Service – Now just restart Apache service as per setup.

Читайте также:  Astra linux radius server

Part 2: Setup Timezone in Single PHP Script

You can also set the timezone for a single php script by defining timezone inside the php script. Simply edit your php script and add date_default_timezone_set(“MMM”) line at top of script. Change MMM with your preferred timezone.

Источник

Set timezone in PHP

The timezone value is important for any website because the date and time values are displayed based on this value. The PHP script uses the timezone value of the web server by default. This timezone value can be changed by modifying the value of the date.timezone directive in the php.ini configuration file or by adding the entry for timezone value inside the .htaccess file or using several built-in functions. Different ways of setting the timezone value in PHP have been shown in this tutorial.

Set the timezone Value by Modifying the php.ini File

One of the easiest ways to set the default timezone is by modifying the date.timezone directive inside the php.ini file. Suppose you want to set the default timezone to ‘Asia/Dhaka’. Open the php.ini file and search for the location of the date.timezone directive. Modify the line by using the following line:

Save the file and restart the web server to set the date and time of the server based on the modified timezone value.

Set the timezone Value by Modifying the .htaccess File

Modifying the .htaccess file is another way to set the default timezone value. Open the .htaccess file and add the following line to set the default timezone value to ‘Asia/Dhaka’:

Save the file and restart the web server to set the date and time of the server based on the modified timezone value.

Set the timezone Value by Using date_default_timezone_set() Function

The date_default_timezone_set() is the built-in PHP function to set the timezone value. The output of all built-in functions of PHP related to the default timezone will be changed after changing the timezone value using the date_default_timezone_set() function. The syntax of this function is given below:

Syntax

This function has only one mandatory argument. This argument sets a particular timezone. It returns True if the valid timezone value is passed in the argument. Otherwise, it returns False. The date_default_timezone_get() function is used to read the current timezone value of the server. So, this function can be used to check the timezone is set properly after setting the new timezone by using the date_default_timezone_set() function.

Example 1: Set the timezone by Using date_default_timezone_set() Function

Create a PHP file with the following script to set the default timezone to ‘Asia/Dhaka’ using the date_default_timezone_set() function. The date_default_timezone_get() function has been used two times to print the timezone value before and after using date_default_timezone_set() function.

// Print the current timezone
echo «The current timezone is » . date_default_timezone_get ( ) . «
» ;
// Change the current timezone
date_default_timezone_set ( ‘Asia/Dhaka’ ) ;
// Print the changed the timezone
echo «The current timezone is changed to » . date_default_timezone_get ( ) . «» ;

The following output shows that the default timezone was UTC, and the timezone has changed to Asia/Dhaka after setting the new timezone:

Читайте также:  Restart process linux script

Set the timezone Value by Using ini_set() Function

The ini_set() is a very useful function of PHP to modify any PHP directive by using a script without accessing the php.ini file. This tutorial discussed earlier that the ‘date.timezone’ directive required to modify to change the current timezone value. So, the ini_set() function can be used to change this directive value. The syntax of this function is given below:

Syntax

The function’s first argument takes the directive name, and the function’s second argument takes the value. It returns a string value on success and a False on failure.

Example 2: Set the timezone by Using the ini_set() Function

Create a PHP file with the following script that will set the default timezone to ‘America/Chicago’ by using the ini_set() function. The date_default_timezone_get() function has been used two times to print the timezone value before and after using ini_set() function.

// Print the current timezone
echo «The current timezone is » . date_default_timezone_get ( ) . «
» ;
// Change the current timezone
ini_set ( ‘date.timezone’ , ‘America/Chicago’ ) ;
// Print the changed timezone
echo «The current timezone is changed to » . date_default_timezone_get ( ) . «» ;

The following output shows that the default timezone was UTC, and the timezone has changed to ‘America/Chicago’ after setting the new timezone:

Set the timezone Value by Using DateTimeZone Class

Using DateTimeZone class is another way to change the default timezone value of the server. The uses of this class for changing the timezone have been shown in the following example:

Example 3: Set the timezone by Using DateTimeZone Class

Create a PHP file with the following script that will change the timezone value two times and print the current date and time based on the current timezone value. The display() function has been defined in the script to print the current timezone value and the current date and time based on the timezone. It has been called for the first time to show the output based on the default timezone, which is ‘UTC’. It has been called the second time to show the output based on the changed timezone, ‘Asia/Dhaka’. It has been called the third time to show the output based on the changed timezone, which is ‘Canada/Atlantic’.

// Create a date object
$date = new DateTime ( ) ;

function display ( )
{
global $date ;
$timezone = $date — > getTimezone ( ) ;
// Print the current timezone and datetime based on timezone
echo «The current timezone is » . $timezone — > getName ( ) . «
» ;
echo «The current date and time is » . $date — > format ( ‘d-M-Y h:i:s’ ) . «
» ;

// Call function to print the output based on default timezone
display ( ) ;

// Change the timezone to ‘Asia/Dhaka’
$date — > setTimezone ( new DateTimeZone ( ‘Asia/Dhaka’ ) ) ;

// Call function to print the output based on the changed timezone
display ( ) ;

// Change the timezone to ‘Canada/Atlantic’
$date — > setTimezone ( new DateTimeZone ( ‘Canada/Atlantic’ ) ) ;

// Call function to print the output based on the changed timezone
display ( ) ;

The following output will appear after executing the previous script:

Conclusion

Five different ways of changing the timezone value are shown in this tutorial. If the PHP user has no permission to change the php.ini or .htaccess file, then the user can use any of the built-in functions discussed in this tutorial to change the timezone value.

Читайте также:  Samsung m4020nd драйвер линукс

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

How to Setup Time Zone for LAMP or LEMP on Ubuntu Linux

When setting up a new LAMP or LEMP server, doing it right the first time is always good. When your servers are configured the first time correctly, you won’t have to revisit configuring basic tasks.

This post shows students and new users how to properly configure LAMP / LEMP time zone on Ubuntu 16.04 LTS servers.

Configuring the server in the correct time zone and dates allows web admins to get logs reports with accurate dates and times, configure time-based configurations, and many more.

So, when setting up your new LAMP or LEMP server, the step below is a great place to start.

Setting Ubuntu Correct Time zone

When installing a Ubuntu server, you’re prompted to choose the correct time zone in which the server will operate. Unfortunately, some webmasters fail to select the correct time zone and breeze through the installation.

Suppose you want to set Ubuntu up at the correct timezone. Or confirm that it’s in the right timezone, do the step below:

First, run the commands below to view all available time zones on the servers and how their city names are formatted.

sudo timedatectl list-timezones

That should give you something like the list below

Africa/Abidjan Africa/Accra Africa/Addis_Ababa Africa/Algiers Africa/Asmara Africa/Bamako Africa/Bangui Africa/Banjul Africa/Bissau Africa/Blantyre Africa/Brazzaville Africa/Bujumbura Africa/Cairo . .

Now scroll down the list until you find the timezone your server is in. Also, please take note of how it’s formatted. For example, New York is going to be America/New York.

Run the commands below to set the server to the correct timezone when ready.

sudo timedatectl set-timezone America/New_York

That should set the server to the city of New York timezone.

The same thing can be configured with the commands below.

sudo dpkg-reconfigure tzdata

This should give you the same list, but this time you can scroll down and select the timezone from the list easily.

Either of the commands above should give you the same results.

Configure PHP / PHP-FPM with Time zone

Now that the server is set up with the correct timezone configure PHP or PHP-FPM with the same timezone.

Run the commands below for LAMP servers to open the Apache2 PHP default config file.

sudo nano /etc/php/7.0/apache2/php.ini

Then scroll down the list and change the line below to the New York timezone.

date.timezone = America/New_York

Save the file and reload Apache2.

sudo systemctl reload apache2.service

Run the commands below for LEMP servers to open the PHP-FPM config file.

sudo nano /etc/php/7.0/fpm/php.ini

Then scroll down the list and change the line below to the New York timezone.

date.timezone = America/New_York

Save the file and reload PHP-FPM

sudo systemctl reload php7.0-fpm.service

You may also like the post below:

Richard W

I love computers; maybe way too much. What I learned I try to share at geekrewind.com.

Источник

Оцените статью
Adblock
detector