HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

date_diff

Description

The date_diff of Date / Time for PHP returns the difference between two DateTime objects.

Syntax

date_diff(
    DateTimeInterface $baseObject,
    DateTimeInterface $targetObject,
    bool $absolute = false
): DateInterval

Parameters

baseObject

The date to compare from.

targetObject

The date to compare to.

absolute

Should the interval be forced to be positive?

Return

Returns the difference between two DateTimeInterface objects.

Examples

1 · baseObject targetObject

<?

$baseObject = date_create("2001-1-1");
$targetObject = date_create("2000-12-31");

$return = date_diff($baseObject, $targetObject);

echo $return->format("%R%a days");
-1 days

2 · absolute

<?

$baseObject = date_create("2001-1-1");
$targetObject = date_create("2000-12-31");
$absolute = true;

$return = date_diff($baseObject, $targetObject, $absolute);

echo $return->format("%R%a days");
+1 days