只改变一个变量的时间值
Change time value of only one variable
我正在尝试使用 php 中的 ->setTime 更改变量的时间值
但是我也想将原始时间值保存在一个单独的变量中。不幸的是,当我改变一个变量时,它也会改变另一个。有什么想法吗?
<?php
//creating a DateTime object
$date = new DateTime("20-May-2015");
$date2 = $date;
echo $date->format("d-M-Y H:i:s") . "\n";
echo $date2->format("d-M-Y H:i:s") . "\n";
echo "\n";
//setting time to a new value
$date->setTime(5, 10);
echo $date->format("d-M-Y H:i:s") . "\n";
echo $date2->format("d-M-Y H:i:s") . "\n";
?>
output
20-May-2015 00:00:00
20-May-2015 00:00:00
20-May-2015 05:10:00
20-May-2015 05:10:00 ***I want this one to stay 20-May-2015 00:00:00***
对象始终作为 references 传递和分配。要将它们分开,只需将它们实例化为 2 个对象或使用 clone
关键字:
$date = new DateTime("20-May-2015");
$date2 = new DateTime("20-May-2015");
// OR
$date2 = clone $date;
echo $date->format("d-M-Y H:i:s") . "\n";
echo $date2->format("d-M-Y H:i:s") . "\n";
echo "\n";
//setting time to a new value
$date->setTime(5, 10);
echo $date->format("d-M-Y H:i:s") . "\n";
echo $date2->format("d-M-Y H:i:s") . "\n";
输出:
20-May-2015 00:00:00
20-May-2015 00:00:00
20-May-2015 05:10:00
20-May-2015 00:00:00
我正在尝试使用 php 中的 ->setTime 更改变量的时间值 但是我也想将原始时间值保存在一个单独的变量中。不幸的是,当我改变一个变量时,它也会改变另一个。有什么想法吗?
<?php
//creating a DateTime object
$date = new DateTime("20-May-2015");
$date2 = $date;
echo $date->format("d-M-Y H:i:s") . "\n";
echo $date2->format("d-M-Y H:i:s") . "\n";
echo "\n";
//setting time to a new value
$date->setTime(5, 10);
echo $date->format("d-M-Y H:i:s") . "\n";
echo $date2->format("d-M-Y H:i:s") . "\n";
?>
output
20-May-2015 00:00:00
20-May-2015 00:00:00
20-May-2015 05:10:00
20-May-2015 05:10:00 ***I want this one to stay 20-May-2015 00:00:00***
对象始终作为 references 传递和分配。要将它们分开,只需将它们实例化为 2 个对象或使用 clone
关键字:
$date = new DateTime("20-May-2015");
$date2 = new DateTime("20-May-2015");
// OR
$date2 = clone $date;
echo $date->format("d-M-Y H:i:s") . "\n";
echo $date2->format("d-M-Y H:i:s") . "\n";
echo "\n";
//setting time to a new value
$date->setTime(5, 10);
echo $date->format("d-M-Y H:i:s") . "\n";
echo $date2->format("d-M-Y H:i:s") . "\n";
输出:
20-May-2015 00:00:00
20-May-2015 00:00:00
20-May-2015 05:10:00
20-May-2015 00:00:00