Last active
April 28, 2021 09:46
-
-
Save Grummfy/6d4f07a86121a1477cdc to your computer and use it in GitHub Desktop.
DateTimeImmutable for php 5.4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (version_compare(PHP_VERSION, '5.5.0', '<')) | |
{ | |
class DateTimeImmutable | |
{ | |
/** | |
* @var DateTime | |
*/ | |
protected $datetime; | |
public function __construct($time = "now", \DateTimeZone $timezone = null) { | |
$this->datetime = new \DateTime($time, $timezone); | |
} | |
public static function createFromMutable( \DateTime $datetime) { | |
$self = new self(); | |
$self->datetime = clone $datetime; | |
return $self; | |
} | |
public function __call($name, $arguments) { | |
$result = call_user_func_array([clone $this->datetime, $name], $arguments); | |
if ($result instanceof \DateTime) | |
{ | |
$self = new self(); | |
$self->datetime = $result; | |
return $self; | |
} | |
return $result; | |
} | |
public static function __callStatic($name, $arguments) { | |
$result = \DateTime::$name($arguments); | |
if ($result instanceof \DateTime) | |
{ | |
$self = new self(); | |
$self->datetime = $result; | |
return $self; | |
} | |
return $result; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Of course, will work only with php5.2+