企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
> DateTimeImmutable类是 PHP 5.5后新增的处理时间格式类,该类与DateTime相同,只是它从不修改自己,而是返回一个新对象。 > DateTimeImmutable类继承了DateTimeInterface接口,因此需要实现该接口中的六个方法,以及继承了该接口中定义的13个预定义常量。 ``` // 类中包含的方法 public __construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] ) public add ( DateInterval $interval ) : DateTimeImmutable public static createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] ) : DateTimeImmutable public static createFromMutable ( DateTime $datetime ) : DateTimeImmutable public static getLastErrors ( void ) : array public modify ( string $modify ) : DateTimeImmutable public static __set_state ( array $array ) : DateTimeImmutable public setDate ( int $year , int $month , int $day ) : DateTimeImmutable public setISODate ( int $year , int $week [, int $day = 1 ] ) : DateTimeImmutable public setTime ( int $hour , int $minute [, int $second = 0 [, int $microseconds = 0 ]] ) : DateTimeImmutable public setTimestamp ( int $unixtimestamp ) : DateTimeImmutable public setTimezone ( DateTimeZone $timezone ) : DateTimeImmutable public sub ( DateInterval $interval ) : DateTimeImmutable 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 ) ``` ``` // 以add方法为例 $interval = new DateInterval('P2W'); //P2W 表示两周,14天 $datetime = new DateTime('2019-07-14 14:00:00'); $datetime->add($interval);// 加14天 // DateTime类会直接修改$datetime对象 dump($datetime); object(DateTime)#227 (3) { ["date"] => string(26) "2019-07-28 14:00:00.000000" ["timezone_type"] => int(3) ["timezone"] => string(13) "Asia/Shanghai" } $date_immutable = new DateTimeImmutable('2019-07-14 14:00:00'); $date_immutable_new = $date_immutable->add($interval); // DateTimeImmutable类没有修改自己的实例化对象,而是返回一个新的对象 // 因此打印$date_immutable对象,还是显示修改之前的值 dump($date_immutable); object(DateTimeImmutable)#226 (3) { ["date"] => string(26) "2019-07-14 14:00:00.000000" ["timezone_type"] => int(3) ["timezone"] => string(13) "Asia/Shanghai" } // 新的对象$date_immutable_new为更改后的值 dump($date_immutable_new); object(DateTimeImmutable)#225 (3) { ["date"] => string(26) "2019-07-28 14:00:00.000000" ["timezone_type"] => int(3) ["timezone"] => string(13) "Asia/Shanghai" } ```