search
HomeBackend DevelopmentPHP TutorialPHP中date与gmdate的区别及默认时区设置_PHP

一、date与gmdate有什么区别?

PHP时间函数中有两个格式化函数:date()和gmdate(),在官方的文档中的描述为:
复制代码 代码如下:date()   #— 格式化一个本地时间/日期
gmdate() #— 格式化一个 GMT/UTC 日期/时间,返回的是格林威治标准时(GMT)。
先来举个例子,我们现在所在的时区是+8,那么服务器运行以下脚本返回的时间应该是这样的:
当前时间假定是2013-03-14 12:15:27
复制代码 代码如下:echo date('Y-m-d H:i:s'); #与date('Y-m-d H:i:s' time());等效,输出为:2007-03-14 12:15:27
echo gmdate('Y-m-d H:i:s'); #与gmdate('Y-m-d H:i:s' time());输出为:2007-03-14 04:15:27
但是这只是在Linux+Apache下运行PHP所得的结果,如果在Windows下运行,则2个函数返回都是:2013-03-14 04:15:27。
所以,我们应该给一个兼容性的写法,统一使用gmdate,并手工设置当前时区,写法改进如下:
复制代码 代码如下:echo gmdate('Y-m-d H:i:s', time() + 3600 * 8);
这样不管在Linux+Apache下还是Windows下都得到了正确的结果,当然这样写还有一个好处,当网站是面向全世界的时候,那么网站用户只要设置所在的时区,程序自动根据用户设置的时区进行时间计算,数据库中信息发布时间只存当前的time()所生成的时间,那么在中国+8时区看到的发布时间是:2007-03-14 12:15:27,那么在欧洲+2时区用户看到这个信息的发布时间是:2007-03-14 06:15:27,这样信息的时间就全部对应正确了。

二、修改PHP的默认时区会影响么?

每个地区都有自己的本地时间,在网上以及无线电通信中,时间的转换问题就显得格外突出。整个地球分为二十四个时区,每个时区都有自己的本地时间。在国际无线电或网络通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC,Universal Time Coordinated),是由世界时间标准设定的全球标准时间。UTC原先也被称为格林威治标准时间(GMT,Greenwich Mean Time),都与英国伦敦的本地时间相同。

PHP默认的时区设置是UTC时间,而北京正好位于时区的东八区,领先UTC八个小时。所以在使用PHP中像time()等获取当前时间的函数时,得到的时间总是不对,表现是和北京时间相差八个小时。如果希望正确的显示北京时间,就需要修改默认的时区设置,可以通过以下两种方式完成。

如果使用的是独立的服务器,有权限修改配置文件,设置时区就可以通过修改php.ini中的date.timezone属性完成。我们可以将这个属性的值设置为”Asia/Shang”、”Asia/Chongqing”、”Etc/GMT-8″或PRC等中的一个,再在PHP脚本中获取的当前时间就是北京时间。修改PHP的配置文件如下所示:
复制代码 代码如下:date.timezone = Etc/GMT-8
//在配置文件中设置默认时区为东8区(北京时间)     

如果您使用的是共享服务器/虚拟主机空间,没有权限修改配置文件php.ini,并且PHP版本又在5.1.0以上,也可以在输出时间之前调用date_default_timezone_set()函数设置时区。该函数需要提供一个时区标识符作为参数,和配置文件中date.timezone属性的值相同。该函数的使用如下所示:
复制代码 代码如下:date_default_timezone_set(‘PRC');         
//在输出时间之前设置时区,PRC为中华人民共和国 echo date(‘Y-m-d H:i:s', time());       
//输出的当前时间为北京时间

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

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: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

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 and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

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 and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

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.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

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 and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

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.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

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

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

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.

See all articles

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)