Reputation: 2773
I have a Convention object with an startDate and an endDate. I want to clone this object allowing to choose a new startDate. Then I'll move all dates of child objects according to this date displacement.
This is what I have:
$newDate = $_POST('newDate');
$originalConventionBeginDate = clone $convention->getBeginDate();
$newConvention = clone $convention;
$jumpInterval = $originalConventionBeginDate->diff($newDate);
error_log(sprintf("Original: %s", print_r($originalConventionBeginDate, true)));
error_log(sprintf("New date: %s", print_r($newDate, true)));
error_log(print_r($jumpInterval, true));
//more code adding $jumpInterval to child objects' dates
foreach ($newConvention->getHallReservation() as $newHallR)
{
$prevDate = clone $newHallR->getDate();
$prevDate->add($jumpInterval);
$newHallR->setDate($prevDate);
}
The result is, if I clone a convention starting in 2018-03-01
to 2018-04-01
, $jumpInterval
is 1 month and 3 days, so, while new Convention is set to start at 2018-04-01
its child objects are set to 2018-04-04
.
This is my log info:
Original: DateTime Object ( [date] => 2018-03-01 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin )
New date: DateTime Object ( [date] => 2018-04-01 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin )
DateInterval Object ( [y] => 0 [m] => 1 [d] => 3 [h] => 0 [i] => 0 [s] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 31 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )
I can see that jumpInterval has also [days] => 31
which seems more accurate than [m] => 1 [d] => 3
, maybe it has something to do that right now we are in February and this month has 28 days??
What is the correct operation over $originalConventionBeginDate
and $newDate
?
Thanks
EDIT: Added the add()
bit to code
Upvotes: 0
Views: 210
Reputation: 1194
I think you should be able to fix this just by changing:
$prevDate->add($jumpInterval);
to:
$prevDate->modify("+$jumpInterval->days days");
Upvotes: -1