- var_dump: documentation ( source)
- print_r: documentation ( source)
- date_default_timezone_set: documentation ( source)
<?php
date_default_timezone_set('Europe/London');
$date3 = DateTime::createFromFormat('Y-m-d H:i:s', '2019-04-01 00:00:00'); // 2019-04-01 00:00:00.0 Europe/London (+01:00)
$date4 = clone $date3;
$date4->modify('+5 week'); // 2019-05-06 00:00:00.0 Europe/London (+01:00)
echo "So the originally modified date with 5 weeks added is:";
print_r($date4);
echo "\n\n";
// positive DIFF and addition
echo "1. positive DIFF and addition\n";
echo "diff is: ";
$positiveDifferenceDateInterval2 = $date3->diff($date4); // interval: + 1m 4d; days 35
print_r($positiveDifferenceDateInterval2->format('%R %mm %dd'));
echo "; days: {$positiveDifferenceDateInterval2->days} \n";
$positiveAddedDate = clone $date3;
$positiveAddedDate->add($positiveDifferenceDateInterval2);
print_r($positiveAddedDate);
echo "is it equal to \$date4? ";
var_dump($date4->getTimestamp() === $positiveAddedDate->getTimestamp());
echo "\n";
// negative DIFF and addition
echo "2. negative DIFF and addition\n";
echo "diff is: ";
$negativeDifferenceDateInterval2 = $date4->diff($date3); // interval: - 1m 5d; days 35
print_r($negativeDifferenceDateInterval2->format('%R %mm %dd'));
echo "; days {$negativeDifferenceDateInterval2->days}";
$negativeAddedDate = clone $date3;
// so in order to make it equal to $date4 I need to subtract a negative interval instead of adding a positive one? how crazy is that?!
$negativeAddedDate->sub($negativeDifferenceDateInterval2);
print_r($negativeAddedDate);
echo "is it equal to \$date4? ";
var_dump($date4->getTimestamp() === $negativeAddedDate->getTimestamp());
echo "\n\n";
echo 'So in order to make it equal to $date4 I need to subtract a negative interval instead of adding a positive one? How crazy is that?!';