Skip to content

Instantly share code, notes, and snippets.

@Grummfy
Last active April 28, 2021 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grummfy/6d4f07a86121a1477cdc to your computer and use it in GitHub Desktop.
Save Grummfy/6d4f07a86121a1477cdc to your computer and use it in GitHub Desktop.
DateTimeImmutable for php 5.4
<?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;
}
}
}
@guiled
Copy link

guiled commented Sep 11, 2015

Of course, will work only with php5.2+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment