date_diff

Returns the difference between two DateTime objects

Syntax

date_diff ( DateTimeInterface $originObject , DateTimeInterface $targetObject [, bool $absolute = FALSE ] ) : DateInterval

Parameters

originObject

The date to compare from.

targetObject

The date to compare to.

absolute

Should the interval be forced to be positive?

Return

The DateInterval object representing the difference between the two dates or FALSE on failure.

Examples

1 · originObject targetObject

<?

$originObject = date_create("2001-1-2");
$targetObject = date_create("2001-1-1");

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

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

?>
-1 days

2 · absolute

<?

$originObject = date_create("2001-1-2");
$targetObject = date_create("2001-1-1");
$absolute = true;

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

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

?>
+1 days
HomeMenu