DateTimeInterface::diffDateTimeImmutable::diffDateTime::diffdate_diffReturns the difference between two DateTime objects
&reftitle.description;
&style.oop;publicDateIntervalDateTimeInterface::diffDateTimeInterfacetargetObjectboolabsolute&false;publicDateIntervalDateTimeImmutable::diffDateTimeInterfacetargetObjectboolabsolute&false;publicDateIntervalDateTime::diffDateTimeInterfacetargetObjectboolabsolute&false;&style.procedural;DateIntervaldate_diffDateTimeInterfacebaseObjectDateTimeInterfacetargetObjectboolabsolute&false;
Returns the difference between two DateTimeInterface
objects.
&reftitle.parameters;
datetime
The date to compare to.
absolute
Should the interval be forced to be positive?
&reftitle.returnvalues;
The DateInterval object represents the
difference between the two dates.
The absolute parameter only affects the
invert property of a
DateInterval object.
The return value more specifically represents the clock-time interval to
apply to the original object ($this or
$originObject) to arrive at the
$targetObject. This process is not always
reversible.
The method is aware of DST changeovers, and hence can return an interval of
24 hours and 30 minutes, as per one of the examples. If
you want to calculate with absolute time, you need to convert both the
$this/$baseObject, and
$targetObject to UTC first.
&reftitle.examples;
DateTimeImmutable::diff example&style.oop;
diff($target);
echo $interval->format('%R%a days');
?>
]]>
&style.procedural;
format('%R%a days');
?>
]]>
&examples.outputs;
DateTimeInterface::diff during DST
changeover
diff($targetTime);
echo $interval->format("%H:%I:%S (Full days: %a)"), "\n";
?>
]]>
&example.outputs;
DateTimeInterface::diff range
The value that the method returns is the exact amount of time to get from
$this to $targetObject.
Comparing January 1st to December 31st returns therefore 364, and not 365,
days (for non-leap years).
diff($targetTime);
echo "Full days: ", $interval->format("%a"), "\n";
?>
]]>
&example.outputs;
DateTime object comparisonDateTimeImmutable and
DateTime objects can be compared using
comparison operators.
$date2);
?>
]]>
&example.outputs;
&reftitle.seealso;
DateInterval::formatDateTime::addDateTime::sub