Home >Backend Development >PHP Problem >How to set system time in php
How to set system time in php: 1. Find the "date.timezone" configuration item and set the desired default time zone; 2. Use the "date_default_timezone_set" function to set the default time zone used by all date and time functions in a script .
Recommended: "PHP Video Tutorial"
Manipulating date and time is a very common programming task, but when operating Before, we should ensure the consistency of the time zone, that is, the time zone used for the date and time value to be operated on should be consistent with the default time zone used by the PHP date and time function, otherwise the results will not be what we expect. In PHP, there are many ways to set the system default time zone. We will introduce them one by one below:
Find the date.timezone
configuration item, remove the preceding semicolon and set the desired default time zone.
Before modification:
[Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone ;date.timezone =
After modification:
[Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone = Asia/Shanghai
is used to set all date and time functions in a script The default time zone to use.
date_default_timezone_set( string $timezone_identifier) : bool
<?php $timezone = date_default_timezone_get(); // 获取默认时区 echo 'default timezone is ', $timezone, "\n"; if ($timezone !== 'Asia/Shanghai') { date_default_timezone_set('Asia/Shanghai'); // 设置默认时区 } echo 'current timezone is ', date_default_timezone_get(), "\n"; ?>
is used to modify a certain php.ini# at runtime ## Configuration value.
ini_set( string $varname, string $newvalue) : string
<?php echo 'date_default_timezone_get return ', date_default_timezone_get(), "\n"; $timezone = ini_get('date.timezone'); // 获取默认时区配置选项设置 echo 'date.timezone = ', $timezone, "\n"; if ($timezone !== 'Asia/Shanghai') { ini_set('date.timezone', 'Asia/Shanghai'); // 设置默认时区 } echo 'date.timezone = ', ini_get('date.timezone'), "\n"; echo 'date_default_timezone_get return ', date_default_timezone_get(), "\n"; ?>
date_default_timezone_set() function to set the default time zone. In addition, the deprecated time zone identifiers
Asia/Chongqing and
PRC should no longer be used in new programs.
The above is the detailed content of How to set system time in php. For more information, please follow other related articles on the PHP Chinese website!