处理 日期和时间需要考虑很多事情,例如日期的格式、时区、闰年和天数各异的月份,自己处理太容易出错了,我们应该使用PHP 5.2.0引入的DateTime、DateIntervel和DateTimeZone这些类帮助我们创建及处理日期、时间和时区。
设置默认时区
首先我们要为PHP中处理日期和时间的函数设置默认时区,如果不设置的话,PHP会显示一个 E_WARNING消息,设置默认时区有两种方式,可以像下面这样在 php.ini中设置:
date.timezone = 'Asia/Shanghai';
也可以在运行时使用 date_default_timezone_set()函数设置:
<?phpdate_default_timezone_set('Asia/Shanghai');
这两种方式都要求使用有效的时区标识符,PHP完整的时区标识符可以在这里找到: http://php.net/manual/zh/timezones.php
DateTime类
DateTime类提供了一个面向对象接口,用于管理日期和时间,一个DateTime实例表示一个具体的日期和时间,DateTime构造方法是创建DateTime新实例最简单的方式:
<?php$datetime = new DateTime();
如果没有参数,DateTime类的构造方法创建的是一个表示当前日期和时间的实例。我们可以把一个字符串传入DateTime类的构造方法以便指定日期和时间:
<?php$datetime = new DateTime('2016-06-06 10:00 pm');
注:传入的字符串参数必须是有效的日期和时间格式( http://php.net/manual/zh/datetime.formats.php)
理想情况下,我们会指定PHP能理解的日期和时间格式,可是实际情况并不总是如此,有时我们必须处理其它格式或出乎意料的格式,这时我们可以通过DateTime提供的静态方法 createFromFormat来使用自定义的格式创建DateTime实例,该方法的第一个参数是表示日期和时间格式的字符串,第二个参数是要使用这种格式的日期和时间字符串:
<?php$datetime = DateTime::createFromFormat('M j, Y H:i:s', 'June 6, 2016 22:00:00');
注:也许你很眼熟,没错, DateTime::createFromFormat和 date函数类似。可用的日期时间格式参考这里: http://php.net/manual/zh/datetime.createfromformat.php
DateInterval类
处理DateTime实例之前需要先了解DateInterval类,DateInterval实例表示长度固定的时间段(比如两天),或者相对而言的时间段(例如昨天),我们通常使用该类的实例来修改DateTime实例。例如,DateTime提供了用于处理DateTime实例的 add和 sub方法,这两个方法的参数是一个DateInterval实例,表示从DateTime中增加的时间量或减少的时间量。
我们使用构造函数实例化DateInterval实例,DateInterval构造函数的参数是一个表示时间间隔约定的字符串,这个时间间隔约定以字母P开头,后面跟着一个整数,最后是一个周期标识符,限定前面的整数。有效周期标识符如下:
- Y(年)
- M(月)
- D(日)
- W(周)
- H(时)
- M(分)
- S(秒)
间隔约定中既可以有时间也可以有日期,如果有时间需要在日期和时间之间加上字母T,例如,间隔约定P2D表示间隔两天,间隔约定P2DT5H2M表示间隔两天五小时两分钟。
下面的实例演示了如何使用 add方法将DateTime实例表示的日期和时间向后推移一段时间:
<?php//创建DateTime实例$datetime = new DateTime('2016-06-06 22:00:00');//创建长度为两天的间隔$interval = new DateInterval('P2D');//修改DateTime实例$datetime->add($interval);echo $datetime->format('Y-m-d H:i:s');
我们还可以创建反向的DateInterval实例:
<?php$datetime = new DateTime();$interval = DateInterval::createFromDateString('-1 day');$period = new DatePeriod($datetime, $interval, 3);foreach ($period as $date) { echo $date->format('Y-m-d'), PHP_EOL;}
以上代码输出为:
2016-06-062016-06-052016-06-042016-06-03
DateTimeZone类
PHP使用DateTimeZone类表示时区,我们只需要把有效的时区标识符传递给DateTimeZone类的构造函数:
<?php$timezone = new DateTimeZone('Asia/Shanghai');
创建DateTime实例通常需要使用DateTimeZone实例,DateTime类构造方法的第二个参数(可选)就是一个DateTimeZone实例,传入这个参数后,DateTime实例的值以及对这个值的所有修改都相对于这个指定的时区,如果不传入则使用的是前面设置的默认时区:
<?php$timezone = new DateTimeZone('Asia/Shanghai');$datetime = new DateTime('2016-06-06', $timezone);
实例化之后还可以使用 setTimezone方法修改DateTime实例的时区:
<?php$timezone = new DateTimeZone('Asia/Shanghai');$datetime = new DateTime('2016-06-06', $timezone);$datetime->setTimezone(new DateTimeZone('Asia/Hong_kong'));
DatePeriod类
有时我们需要迭代处理一段时间内反复出现的一系列日期和时间,DatePeriod类可以解决这个问题(前面已经用到过),DatePeriod类的构造方法接受三个参数而且都必须提供:
- 一个DateTime实例,表示迭代开始的日期和时间
- 一个DateInterval实例,表示下一个日期和时间的间隔
- 一个整数,表示迭代的总次数
DatePeriod是迭代器,每次迭代都会产出一个DateTime实例。DatePeriod的第四个参数是可选的,用于显式指定周期的结束日期和时间,如果迭代时想要排除开始日期和时间,可以把构造方法的最后一个参数设为 DatePeriod::EXCLUDE_START_DATE常量:
<?php$datetime = new DateTime();$interval = new DateInterval('P2D');$period = new DatePeriod($datetime, $interval, 3, DatePeriod::EXCLUDE_START_DATE);foreach ($period as $date) { echo $date->format('Y-m-d H:i:s'), PHP_EOL;}
打印的结果是:
2016-06-082016-06-102016-06-12
nesbot/carbon日期组件
如果经常需要处理日期和时间,应该使用 nesbot/carbon组件( https://github.com/briannesbitt/Carbon),Laravel框架也是使用了这个组件处理日期和时间,该组件集成了常用的日期及时间处理API,其底层正是使用了我们上面提到的几个日期时间处理类实现了各种功能,有兴趣可以去研究下。

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

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

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools