Related Articles

Related Articles

How to copy a DateTime object in PHP ?
  • Last Updated : 23 Apr, 2020

Given a DateTime object and the task is to create a copy of that object. To create a copy of DateTime object we use clone keyword which is described below:

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. Any properties that are references to other variables will remain references.

Syntax:

$DateTime_copy_object_name = clone $DateTime_object_to_be_copied

Below examples illustrate the cloning of DateTime object in PHP:

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Program to create copy of DateTime object 
  
// Creating object 
$obj = new DateTime();
  
// Creating clone or copy of object 
$copy = clone $obj
  
// Printing inital values
echo "Printing inital values\n";
  
$result = $obj->format('Y-m-d H:i:s');
echo "Oiginal object: $result\n";
  
$result = $copy->format('Y-m-d H:i:s');
echo "Cloned object: $result\n\n";
  
  
// Changing original object ($obj) value
// to verify other($copy) is clone or not
  
// This statement will add 3 Days
$obj->add(new DateInterval('P3D'));
  
// Printing values
echo "Printing values after change\n";
  
$result = $obj->format('Y-m-d H:i:s');
echo "Oiginal object: $result\n";
  
  
$result = $copy->format('Y-m-d H:i:s');
echo "Cloned object: $result\n";
?>

chevron_right


Output:

Printing inital values
Oiginal object: 2020-04-14 17:14:18
Cloned object: 2020-04-14 17:14:18

Printing values after change
Oiginal object: 2020-04-17 17:14:18
Cloned object: 2020-04-14 17:14:18

Example 2: Below program distinguishes clone from assignment ( = ) operator.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Program to create copy of DateTime object 
  
// Creating object 
$obj = new DateTime();
  
// Creating clone or copy of object 
$copy = clone $obj;
  
$ref_obj = $obj;
  
// Printing inital values
echo "Printing inital values\n";
  
$result = $obj->format('Y-m-d H:i:s');
echo "Oiginal object: $result\n";
  
$result = $ref_obj->format('Y-m-d H:i:s');
echo "Reference object: $result\n";
  
$result = $copy->format('Y-m-d H:i:s');
echo "Cloned object: $result\n\n";
  
  
// Changing original object ($obj) value
// to verify other($copy) is clone or not
  
// This statement will add 3 Months
$obj->add(new DateInterval('P3M'));
  
// Printing values
echo "Printing values after change\n";
  
$result = $obj->format('Y-m-d H:i:s');
echo "Oiginal object: $result\n";
  
$result = $ref_obj->format('Y-m-d H:i:s');
echo "Reference object: $result\n";
  
$result = $copy->format('Y-m-d H:i:s');
echo "Cloned object: $result\n";
?>

chevron_right


Output:

Printing inital values
Oiginal object: 2020-04-14 17:14:49
Reference object: 2020-04-14 17:14:49
Cloned object: 2020-04-14 17:14:49

Printing values after change
Oiginal object: 2020-07-14 17:14:49
Reference object: 2020-07-14 17:14:49
Cloned object: 2020-04-14 17:14:49

So, to make a copy of the object we use clone, don’t use (=) operator as it creates a reference.




My Personal Notes arrow_drop_up
Recommended Articles
Page :