doumianfeng5065 2014-08-27 02:22 采纳率: 0%
浏览 45
已采纳

date_time_set问题有2个不同的日期

I'm trying to set the time between 2 variables but when I check it copies the change on the 2 time. For example, I select 1:00 and 23:00 , both dates show 23:00 only. I can't seem to get why it's showing like that even if I have already different variables for each time. I need to show the different dates.

Input Dates:

01:30 and 21:00

My code:

$date = $_POST['datepicker'];

$time1 = $_POST['timepicker'];
$time2 = $_POST['timepicker2'];

$time1_array = explode(":",$time1);
$time2_array = explode(":",$time2);

$date = $_POST['datepicker'];

$date = new DateTime($date);
$date2 = $date;
$datefinal1 = date_time_set($date, $time1_array[0], $time1_array[1], 00);
$datefinal2 = date_time_set($date2,  $time2_array[0], $time2_array[1], 59);
$result = $datefinal1->format('Y-m-d H:i:s');
$result2 = $datefinal2->format('Y-m-d H:i:s');
print_r($datefinal1);

Sample output that I am getting:

DateTime Object ( [date] => 2015-03-17 21:00:59 [timezone_type] => 3 [timezone] => Europe/Berlin )

even if I should be getting the $datefinal2's value.

  • 写回答

1条回答 默认 最新

  • dongyue9864 2014-08-27 02:24
    关注

    Objects are copied by reference, not value. If you assign one object to a new variable both point to the same object so changing one changes the other. To copy an object you need to use clone.

    $date2 = $date;
    

    should be:

    $date2 = clone $date;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP