Home Backend Development PHP Tutorial Summary of setting time zone in PHP_PHP tutorial

Summary of setting time zone in PHP_PHP tutorial

Jul 21, 2016 pm 03:18 PM
php about reason back exist turn up search method Time zone of set up

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

1. Modify php.ini, find data.timezone in php.ini = 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 version 5 or above;

Explanation 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 setting time zone method:

Copy the code The code is as follows:

function_exists(date_default_timezone_set);//Here he always returns 1, this function is to determine the characters inside Is it a defined function name?
date_default_timezone_set("Etc/GMT");//This is Greenwich Mean Time, and the time obtained is the same as the default time zone
date_default_timezone_set("Etc/GMT+8 ");//This is 8 hours ahead of Ringwich Standard Time
date_default_timezone_set("Etc/GMT-8");//This is 8 hours ahead of Ringwich Standard Time
date_default_timezone_set('PRC') ; //Set the Chinese time zone
?>


Function ini_set() to set the time zone:
You can add ini_set('date. timezone','Asia/Shanghai'); // 'Asia/Shanghai' is the Shanghai time zone

Manually modify the php.ini settings
Open php and find date.timezone = "PRC " If necessary, 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?/My It’s 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() in the header to set my default time zone to Beijing time
Copy code Code As follows:

date_default_timezone_set('PRC');
echo date('Y-m-d H:i:s');
?>

The time is the same as the current time of the server!
In addition, the usage of date_default_timezone_set 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() Sets the default timezone used 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 invalid, 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() in the header to set my default time zone to Beijing time
Copy code Code As follows:

date_default_timezone_set('PRC');
echo date('Y-m-d H:i:s');
?>

The time is the same as the current time of the server!
In addition, the usage of date_default_timezone_set is as follows:
-------------------------- -------------------------------------------------- -------
date_default_timezone_set
(PHP 5 >= 5.1.0RC1)
date_default_timezone_set -- Set the default time zone used for all date and time functions in a script
Description
bool date_default_timezone_set ( string timezone_identifier )
date_default_timezone_set() Sets the default timezone used 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 invalid, 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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325412.htmlTechArticleAfter finding the reason, I searched on the Internet and found some time zone setting methods for PHP: 1. Modify php.ini, in Find data.timezone in php.ini = remove the ; sign in front of it, and then set data.timezo...
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles