Home Backend Development PHP Tutorial Summary of setting time zone methods in PHP

Summary of setting time zone methods in PHP

Dec 30, 2016 am 10:38 AM
Time zone

After finding the reason, I searched for some time zone setting methods for PHP on the Internet:

1. Modify php.ini, find data.timezone = in php.ini and remove the ; sign in front of it, and then Set data.timezone = “Asia/Shanghai”; that’s it.

2. Use the function ini_set('date.timezone','Asia/Shanghai'); or date_default_timezone_set('Asia/Shanghai'); in the program code of PHP 5 or above version

Description of some commonly used time zone identifiers:

Asia/Shanghai – Shanghai
Asia/Chongqing – Chongqing
Asia/Urumqi – Urumqi
Asia/Hong_Kong – Hong Kong
Asia/Macao – Macau
Asia/Taipei – Taipei
Asia/Singapore – Singapore


Function to set time zone:

<?php 
function_exists(date_default_timezone_set);//在这他总是返回1,这函数是判断这里面的字符是不是一个定义了的函数名 
date_default_timezone_set("Etc/GMT");//这是格林威治标准时间,得到的时间和默认时区是一样的 
date_default_timezone_set("Etc/GMT+8");//这里比林威治标准时间慢8小时 
date_default_timezone_set("Etc/GMT-8");//这里比林威治标准时间快8小时 
date_default_timezone_set(&#39;PRC&#39;); //设置中国时区 
?>
Copy after login

Function ini_set() to set time zone:
Yes Add ini_set('date.timezone','Asia/Shanghai') at the beginning of the file; // 'Asia/Shanghai' is the Shanghai time zone

Manually modify the php.ini settings
Open php and find date.timezone = "PRC" If there is one, remove the semicolon in front of it, if not, add it manually!

The following is some additional information:

You will find this problem after installing PHP5

$atime=date("Y-m-d H:i:s");
echo $atime;
?>
Output: 2006-05-16 06:36:06
What time is it now?/Mine is 14:36 ​​
Why is this?
The reason is that if you do not set the local time zone of your server in the program or configuration file
The time taken by PHP is Greenwich Mean Time, so it will be different from your local time
Greenwich The difference between standard time and Beijing time is about 8 hours. So how can we avoid time errors?
Let’s take a look at the solution:
Use date_default_timezone_set() on the header of the page to set my default time zone to Beijing time

<? 
date_default_timezone_set(&#39;PRC&#39;); 
echo date(&#39;Y-m-d H:i:s&#39;); 
?>
Copy after login
Copy after login

The time is the same as the current time of the server!
In addition, date_default_timezone_set is attached Usage is as follows:
-------------------------------------------------- -------------------------------------
date_default_timezone_set
(PHP 5 >= 5.1.0RC1)
date_default_timezone_set -- Set the default time zone for all date and time functions in a script
Description
bool date_default_timezone_set ( string timezone_identifier )
date_default_timezone_set() Set for Default time zone for all datetime functions.
Note: Since PHP 5.1.0 (the date and time functions have been rewritten in this version), if the time zone is illegal, each call to the date and time function will generate an E_NOTICE level error message.
Parameters
timezone_identifier
Time zone identifier, such as UTC or Europe/Lisbon
Return value
This function always returns TRUE (even if the timezone_identifier parameter is illegal).
-------------------------------------------------- -------------------------------------
Or modify date.timezone in php.ini Value
date.timezone = PRC

After installing PHP5 you will find this problem
$atime=date("Y-m-d H:i:s");
echo $atime;
?>
Output: 2006-05-16 06:36:06
What time is it now?/Mine is 14:36 ​​
Why is this?
The reason is that if you do not set the local time zone of your server in the program or configuration file
The time taken by PHP is Greenwich Mean Time, so it will be different from your local time
Greenwich The difference between standard time and Beijing time is about 8 hours. So how can we avoid time errors?
Let’s take a look at the solution:
Use date_default_timezone_set() on the header of the page to set my default time zone to Beijing time

<? 
date_default_timezone_set(&#39;PRC&#39;); 
echo date(&#39;Y-m-d H:i:s&#39;); 
?>
Copy after login
Copy after login

The time is the same as the current time of the server!
In addition, date_default_timezone_set is attached Usage is as follows:
-------------------------------------------------- -------------------------------------
date_default_timezone_set
(PHP 5 >= 5.1.0RC1)
date_default_timezone_set -- Set the default time zone for all date and time functions in a script
Description
bool date_default_timezone_set ( string timezone_identifier )
date_default_timezone_set() Set for Default time zone for all datetime functions.
Note: Since PHP 5.1.0 (the date and time functions have been rewritten in this version), if the time zone is illegal, each call to the date and time function will generate an E_NOTICE level error message.
Parameters
timezone_identifier
Time zone identifier, such as UTC or Europe/Lisbon
Return value
This function always returns TRUE (even if the timezone_identifier parameter is illegal).
-------------------------------------------------- -----------------------------------------------
Or modify date.timezone in php.ini Value
date.timezone = PRC

For more summary of time zone setting methods in PHP and related articles, please pay attention to the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set the time zone to Japan in php How to set the time zone to Japan in php Mar 20, 2023 pm 04:08 PM

A time zone refers to the standard time used in a region on Earth. There are 24 time zones in the world, from UTC-12 to UTC+14. When traveling between countries with different time zones, you need to adjust your time according to each country's time zone. PHP has some built-in time functions that allow the time in any time zone around the world to be used.

Convert Unix timestamp to time format using time.Unix function and set time zone Convert Unix timestamp to time format using time.Unix function and set time zone Jul 25, 2023 pm 12:28 PM

Title: Use the time.Unix function to convert Unix timestamps to time format and set the time zone. In the Go language, we often need to convert Unix timestamps into a readable time format, and often need to consider setting the time zone. This article will introduce how to use the Unix function in the time package to accomplish this task, and demonstrate how to set the time zone. First, we need to understand what Unix timestamp means. A Unix timestamp is an integer in seconds representing the time since January 1, 1970

How to select the time zone for Tencent Conference reservations - How to select the time zone for Tencent Conference reservations How to select the time zone for Tencent Conference reservations - How to select the time zone for Tencent Conference reservations Mar 04, 2024 am 11:04 AM

Friends, do you know how to select the time zone when booking a Tencent meeting? Today, the editor will explain how to select the time zone when booking a Tencent meeting. If you are interested, come and take a look with the editor. I hope it can help you. Step one: Enter the Tencent Meeting APP and click to book a meeting (as shown in the picture). Step 2: Select the meeting type, such as regular meeting, and click Next (as shown in the picture). Step 3: On the meeting reservation page, click on the time zone (as shown in the picture). Step 4: Select the time zone (as shown in the picture). Step 5: If the setting is successful, click Finish (as shown in the picture).

How to change time zone in php How to change time zone in php Nov 24, 2022 am 09:28 AM

How to modify the time zone in PHP: 1. Set the default time zone to East 8 in the configuration file; 2. Set the time zone through the code "date_default_timezone_set('Asia/Shanghai');"; 3. Set "date_default_timezone_set(' in the PHP page header PRC');".

What should I do if the time zone modification in Linux does not take effect? What should I do if the time zone modification in Linux does not take effect? Mar 25, 2023 am 10:18 AM

Solution to changing the time zone in Linux does not take effect: 1. Log in to the Linux system and enter the terminal; 2. Copy the configuration file to modify the time zone; 3. View the hardware clock through the "hwclock --show" command, and then set the hardware clock; 4. Pass "vim /etc/timezone Asia/Shanghai" can modify the time zone.

How to change time zone in php.ini file How to change time zone in php.ini file Mar 22, 2023 pm 03:22 PM

PHP is a very popular server-side programming language. When developing web applications using PHP, we sometimes need to set the time zone in PHP. The default time zone of PHP is "UTC (Coordinated Universal Time)", which is not the time zone we want in many cases, so we need to change the time zone setting in the php.ini file. This article will explain how to change the time zone in the php.ini file.

How to adjust time zone in Golang? How to adjust time zone in Golang? Jun 03, 2024 am 11:56 AM

How to adjust time zone in Go? Use time.LoadLocation to load the time zone. Use the In method to convert the time to a specified time zone. Use the Format method to format the time into a string. Use the Parse method to parse a time string with time zone information. Get the client time zone in the HTTP request through Header.Get("TimeZone").

Symphony of Time: The timestamp mystery of the PHP DateTime extension Symphony of Time: The timestamp mystery of the PHP DateTime extension Mar 08, 2024 am 10:13 AM

The Nature of a DateTime Timestamp In the PHPDateTime extension, a timestamp is a numeric value that represents a specific point in time, usually in the form of a UNIX timestamp, which is the number of seconds that have elapsed since January 1, 1970 00:00:00 UTC. The Mystery of Timestamps: UTC and Time Zones One of the mysteries of DateTime timestamps is that it uses the UTC (Coordinated Universal Time) time zone by default. This means that the timestamp you get may differ from your local time zone. For example, if you get a timestamp in Pacific Time (UTC-8), it will be 8 hours behind local time. To solve this mystery, you can use the DateTime::setTimestamp() method to specify a specific

See all articles