Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

comsolit/date-time-immutable

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DateTimeImmutable polyfill

Build Status

This small library adds a polyfill for the DateTimeImmutable object introduced in PHP 5.5.0.

Authors

  • Chris Wilkinson

It's partially based on Benjamin Eberlei's Immutable DateTime Objects blog post.

Installation

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

Basic usage

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

Caveats

PHP 5.5.0 also introduced a DateTimeInterface which both DateTimeImmutable and DateTime implement. In this polyfill we can't change the DateTime class, so DateTimeImmutable has to extend it. This is slightly dangerous as their behaviour is not compatible (see https://bugs.php.net/bug.php?id=64513).

PHP 5.6.0 added a DateTimeImmutable::createFromMutable() factory method, which this polyfill doesn't include.

About

Polyfill for the DateTimeImmutable class added in PHP 5.5.0

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%