comsolit/date-time-immutable

此包已被废弃,不再维护。未建议替代包。

PHP 5.5.0 中添加的 DateTimeImmutable 类的填充程序

v2.0.0+alpha1 2015-03-19 12:19 UTC

This package is auto-updated.

Last update: 2022-10-29 02:06:55 UTC


README

Build Status

这个小型库为在 PHP 5.5.0 中引入的 DateTimeImmutable 对象添加了填充程序。

作者

  • Chris Wilkinson

它部分基于 Benjamin Eberlei 的不可变 DateTime 对象博客文章。

安装

$ php composer.phar require thewilkybarkid/date-time-immutable:~1.0

基本用法

$dateTime = new DateTimeImmutable();
$newDateTime = $datetime->modify('+1 day');
var_dump($dateTime === $newDateTime); // output 'bool(false)'

注意事项

PHP 5.5.0 还引入了 DateTimeInterface,它由 DateTimeImmutableDateTime 实现。在这个填充程序中,我们无法更改 DateTime 类,因此 DateTimeImmutable 必须扩展它。这有点危险,因为它们的行为不兼容(见 https://bugs.php.net/bug.php?id=64513)。

PHP 5.6.0 添加了 DateTimeImmutable::createFromMutable() 工厂方法,但此填充程序不包括。