The content of this article is about setting the time zone in PHP. Friends in need can refer to it.
Today I found something wrong with the time judgment statement in a piece of PHP code, and I studied it. I found that the problem lies in the time zone setting of PHP. The time taken by PHP defaults to Greenwich Mean Time, so it is 8 hours different from Beijing time.
After finding the reason, I searched for some time zone settings of PHP on the Internet. Method:
1. Modify php.ini, find data.timezone = in php.ini and remove the ";" in front of it, and then set data.timezone = "Asia/Shanghai";.
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 method to set time zone:
Copy the code The code is as follows:
<?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('PRC'); //设置中国时区 ?>
Function ini_set() sets 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 any Remove the semicolon in front, 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 of the page to set my default time zone to Beijing time
Copy code The code is 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 time zone 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 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() in the header of the page to set my default time zone to Beijing time
Copy code The code is as follows:
<? date_default_timezone_set('PRC'); echo date('Y-m-d H:i:s'); ?>
时间和服务器当前时间一样了!
另外附date_default_timezone_set用法如下:
------------------------------------------------------------------------------------
date_default_timezone_set
(PHP 5 >= 5.1.0RC1)
date_default_timezone_set -- 设定用于一个脚本中所有日期时间函数的默认时区
说明
bool date_default_timezone_set ( string timezone_identifier )
date_default_timezone_set() 设定用于所有日期时间函数的默认时区。
注: 自 PHP 5.1.0 起(此版本日期时间函数被重写了),如果时区不合法则每个对日期时间函数的调用都会产生一条 E_NOTICE 级别的错误信息。
参数
timezone_identifier
时区标识符,例如 UTC 或 Europe/Lisbon
返回值
本函数永远返回 TRUE(即使 timezone_identifier 参数不合法)。
-------------------------------------------------------------------------------------
或者修改 php.ini 中 date.timezone 值
date.timezone = PRC
相关推荐:
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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment