Open In App

How to copy a DateTime object in PHP ?

Last Updated : 20 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:  

PHP




<?php
 
// Program to create copy of DateTime object
 
// Creating object
$obj = new DateTime();
 
// Creating clone or copy of object
$copy = clone $obj;
 
// Printing initial values
echo "Printing initial values\n";
 
$result = $obj->format('Y-m-d H:i:s');
echo "Original 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 "Original object: $result\n";
 
 
$result = $copy->format('Y-m-d H:i:s');
echo "Cloned object: $result\n";
?>


Output: 

Printing initial values
Original object: 2020-04-14 17:14:18
Cloned object: 2020-04-14 17:14:18

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

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

PHP




<?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 initial values
echo "Printing initial values\n";
 
$result = $obj->format('Y-m-d H:i:s');
echo "Original 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 "Original 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";
?>


Output: 

Printing initial values
Original 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
Original 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.
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads