Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deep copy a DateTime object?

Tags:

php

datetime

People also ask

How do I copy a DateTime object?

A copy of DateTime object is created by using the clone keyword (which calls the object's __clone() method if possible). An object's __clone() method cannot be called directly. When an object is cloned, PHP will perform a shallow copy of all of the object's properties.

Is DateTime immutable in Python?

Since all available types in the datetime module are documented as being immutable (right after the documentation of the classes it is stated): Objects of these types are immutable. you shouldn't worry about this.


$date1 = new DateTime();
$date2 = new DateTime();
$date2->add(new DateInterval('P3Y'));

Update:

If you want to copy rather than reference an existing DT object, use clone, not =.

$a = clone $b;


Clone the date with the clone operator:

$date1 = new DateTime();
$date2 = clone $date1;
$date2->add(new DateInterval('P3Y'));

Clones are shallow by default, but deep enough for a DateTime. In your own objects, you can define the __clone() magic method to clone the properties (i.e. child objects) that make sense to be cloned when the parent object changes.

(I'm not sure why the documentation thinks a good example of needing to clone an object is GTK. Who uses GTK in PHP?)


PHP 5.5.0 introduced DateTimeImmutable. add and modify methods of this class return new objects.

$date1 = new DateTimeImmutable();
$date2 = $date1->add(new DateInterval('P3Y'));

TLDR:

$date1 = new DateTime();
$date2 = (clone $date1)->modify('+3 years');

(Shallow copy is enaugh - Deep copy-ing DateTime makes (currently) no sense)

Simple as that :)

Explanation "php create datetime object from another datetime":

  1. The clone keyword makes regular shallow copy - enaugh for this case (why => see below)
  2. Wraping it with () evaluates the expression returning the newly created object by clone
  3. ->modify() is therefore called on and modifies the new object
  4. DateTime::modify(...) docs:

    Returns the DateTime object for method chaining or FALSE on failure.

  5. $date2 now contains the newly created & modified clone/copy, while $date1 remains unchanged

Why you don't need to deep copy here:

Deep copy/clone is only necessary, when you need to copy targets of properties that are references, but this:

class TestDateTime extends DateTime{
  public function test(){
   //*this* way also outputs private variables if any...
   var_dump( get_object_vars($this) );    
  }
}
$test = (new TestDateTime())->test();

outputs:

array(3) {
  ["date"]=>
  string(26) "2019-08-21 11:38:48.760390"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}

so there are no references, just simple types => no need to deep copy.


You should change your DateTime to DateTimeImmutable

// from date time
$date = \DateTimeImmutable::createFromMutable($mutableDate)

then you can call any method on the DateTime without worrying about it change