Home > Backend Development > PHP Problem > How to set time zone in php

How to set time zone in php

藏色散人
Release: 2023-03-08 20:44:02
Original
11968 people have browsed it

How to set the time zone in php: 1. Modify the value of "date.timezone" in the configuration file "php.ini"; 2. Use the function "date_default_timezone_set" to temporarily set the time zone.

How to set time zone in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

Time zone setting date.timezone in PHP

For the same timestamp, if the time zone is different, the obtained date and time string will also be different. Therefore, setting the correct time zone is essential.

Different time zones will affect the value of date('Y-m-d H:i:s', time()) or similar functions.

There are two ways to set the time zone:

1. Modify the value of date.timezone in the configuration file php.ini

date.timezone=Asia/Shanghai
Copy after login

or

date.timezone=Asia/Chongqing
Copy after login

It is set to the Shanghai time zone in Asia, which is China's time zone. After restarting the web server, it will take effect permanently.

2. Use the function to temporarily set the time zone

bool  date_default_timezone_set  (   string $timezone_identifier  )
Copy after login

Returns true if the setting is successful, false if it fails.

date_default_timezone_set( 'Asia/Shanghai' );
Copy after login

Function date_default_timezone_get() can get the time zone in effect at the function call and return the time zone string.

【Recommended: PHP Video Tutorial

The above is the detailed content of How to set time zone in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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