thewilkybarkid/date-time-immutable

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

PHP 5.5.0 中添加的 DateTimeImmutable 类的 polyfill

v1.0.2 2014-10-23 09:01 UTC

This package is auto-updated.

Last update: 2022-02-01 12:27:59 UTC


README

Build Status

这个小库为 PHP 5.5.0 中引入的 DateTimeImmutable 对象添加了 polyfill。

作者

  • 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 实现。在这个 polyfill 中,我们无法更改 DateTime 类,所以 DateTimeImmutable 必须扩展它。这稍微有点危险,因为它们的行为不兼容(参见 https://bugs.php.net/bug.php?id=64513)。

PHP 5.6.0 添加了一个 DateTimeImmutable::createFromMutable() 工厂方法,这个 polyfill 不包括。