> DateTime 类是 PHP 5.2后新增的处理时间格式类,可以输出、转换、计算时间,很好的替代了 date() 、strtotime()等函数。
> DateTime类继承了DateTimeInterface接口,因此需要实现该接口中的六个方法,以及继承了该接口中定义的13个预定义常量。
```
// 类中包含的方法
public __construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )
public add ( DateInterval $interval ): DateTime
public static createFromFormat ( string $format , string $time [, DateTimeZone $timezone ]: DateTime
public static createFromImmutable ( DateTimeImmutable $datetime ) : DateTime // 需要php 7.3以上版本
public static getLastErrors ( void ) : arraypublic modify ( string $modify ): DateTime
public static __set_state ( array $array ): DateTime
public setDate ( int $year , int $month , int $day ): DateTime
public setISODate ( int $year , int $week [, int $day = 1 ] ): DateTime
public setTime ( int $hour , int $minute [, int $second = 0 [, int $microseconds = 0 ]] ): DateTime
public setTimestamp ( int $unixtimestamp ): DateTime
public setTimezone ( DateTimeZone $timezone ): DateTime
public sub ( DateInterval $interval ): DateTime public diff ( DateTimeInterface $datetime2 [, bool $absolute = FALSE ] ): DateInterval
public format ( string $format ) : string
public getOffset ( void ) : int
public getTimestamp ( void ) : int
public getTimezone ( void ) : DateTimeZone
public __wakeup ( void )
```
输出时间:
```
$date = new DateTime;
echo $date->format('Y-m-d H:i:s'); // 2020-04-04 22:46:26
echo $date->format('Y-m-d'); // 2020-04-04
echo $date->getTimestamp(); // 获取时间戳
echo $date->getOffset(); // 获取时区偏移量
$timezone = $date->getTimezone();
// 获取时区方法,返回DateTimeZone对象
object(DateTimeZone)#227 (2) {
["timezone_type"] => int(3)
["timezone"] => string(13) "Asia/Shanghai"
}
echo $timezone->getName();// Asia/Shanghai
// createFromFormat 方法:根据指定格式分析时间字符串,返回Datetime对象或者false。
// 第一个参数为format字符串格式(参考format参数格式文档),第二个参数字符串需与第一个参数的格式对齐
// 第三个参数为所需时区的DateTimeZone对象。(可选)
$date2 = DateTime::createFromFormat('M j, Y H:i:s', 'Jan 2, 2019 14:14:55');
dump($date2);
object(DateTime)#228 (3) {
["date"] => string(26) "2019-01-02 14:14:55.000000"
["timezone_type"] => int(3)
["timezone"] => string(13) "Asia/Shanghai"
}
// getLastErrors方法,返回分析日期/时间字符串时发现的警告和错误数组。
$date2 = DateTime::createFromFormat('M j, Y', 'Jan 2, 2019 14:14:55'); // 两个参数格式对不上,解析失败
dump(DateTime::getLastErrors());
array(4) {
["warning_count"] => int(0)
["warnings"] => array(0) {
}
["error_count"] => int(1)
["errors"] => array(1) {
[11] => string(13) "Trailing data"
}
}
dump($date2); bool(false)
```
自定义时间:
```
$date = new DateTime('2018-01-01');
echo $date->format('Y-m-d H:i:s');// 2018-01-01 00:00:00
$date1 = new DateTime;
$date1->setTimestamp('1514739661');
echo $date1->format('Y-m-d H:i:s');// 2018-01-01 01:01:01
$date2 = new DateTime('tomorrow');
echo $date2->format('Y-m-d');// 2018-03-14
$date3 = new DateTime('+2 month');
echo $date3->format('Y-m-d');// 2018-03-15
```
编辑时间:
```
date = new DateTime;
$date->setDate(2020, 8, 8); // 设置年月日
$date->setTime(20, 3, 54); // 设置 时分秒
echo $date->format('Y-m-d H:i:s');// 2020-08-08 20:03:54
// modify 修改时间
// 参数:+或- 数量 days、months、years、hours、minutes、seconds
$date3 = new DateTime;
echo $date3->format('Y-m-d H:i:s')."
";// 2020-04-04 23:33:30
$date3->modify('+10 seconds');
echo $date3->format('Y-m-d H:i:s')."
";// 2020-04-04 23:33:40
// setISODate:基于Unix时间戳设置日期和时间。参数:年,星期数,第几天。
$data4 = new DateTime;
$temp = $data4->setISODate(2020, 3, 2);// 2020年第三个星期的第二天
echo $temp->format('Y-m-d')."
";// 2020-01-14
```
计算时间:
```
//diff方法:计算日期差,第二个参数为true时,忽略正负。方法返回DateInterval 对象
// 与date_diff()函数效果一致
$date1 = new DateTime("2018-11-31");
$date2 = new DateTime("2018-11-04");
$interval = $date1->diff($date2,true);
echo $interval->format('%R%a days'); // +27 days
```