How do I add the difference between two DateTime objects to another DateTime object? I tried some code similar to the one below, but it didn’t work.
$first_time=new DateTime('01/01/2000 00:00:00');
$second_time=new DateTime('01/01/2000 00:00:50');
$diff=$first_time->diff($second_time);
$time=new DateTime('01/01/2012 12:00:00');
$time->modify('+'.$diff->format('%s').' seconds');
echo $time;
//Should echo: "01/01/2012 12:00:50"
Can somebody help me out?
format()does not calculate the absolut number of seconds of the Interval, it just give you the values of the intern attributes. Since you want to add, why not simply useadd()?diff()returns a DateInterval object, and this is whatadd()needs.