How to set system time in php

藏色散人
Release: 2023-03-05 11:06:01
Original
3232 people have browsed it

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 .

How to set system time in php

Recommended: "PHP Video Tutorial"

System Time Zone Settings

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:

Method 1: Modify the php.ini configuration file

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 =
Copy after login

After modification:

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Shanghai
Copy after login

Method 2: date_default_timezone_set() function

is used to set all date and time functions in a script The default time zone to use.

Syntax

date_default_timezone_set( string $timezone_identifier) : bool
Copy after login

Example

<?php
$timezone = date_default_timezone_get();           // 获取默认时区
echo &#39;default timezone is &#39;, $timezone, "\n";
if ($timezone !== &#39;Asia/Shanghai&#39;) {
    date_default_timezone_set(&#39;Asia/Shanghai&#39;);    // 设置默认时区 
}
echo &#39;current timezone is &#39;, date_default_timezone_get(), "\n";
?>
Copy after login

Method 3: ini_set() function

is used to modify a certain php.ini# at runtime ## Configuration value.

Syntax

ini_set( string $varname, string $newvalue) : string
Copy after login

Example

<?php
echo &#39;date_default_timezone_get return &#39;, date_default_timezone_get(), "\n";

$timezone = ini_get(&#39;date.timezone&#39;);           // 获取默认时区配置选项设置
echo &#39;date.timezone = &#39;, $timezone, "\n";
if ($timezone !== &#39;Asia/Shanghai&#39;) {
    ini_set(&#39;date.timezone&#39;, &#39;Asia/Shanghai&#39;);  // 设置默认时区
}
echo &#39;date.timezone = &#39;, ini_get(&#39;date.timezone&#39;), "\n";

echo &#39;date_default_timezone_get return &#39;, date_default_timezone_get(), "\n";
?>
Copy after login
Conclusion

Method one is a global setting and takes effect for all scripts, while methods two and three only take effect for the script in which it is located. Generally, it is recommended to use the

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!