Time Difference in PHP

Last Updated On Friday 8th Oct 2021

PHP Time Difference

The date_diff() function returns the difference between two DateTime objects.

Syntax

	date_diff(datetime1, datetime2, absolute)
	

Time Difference in PHP

  • datetime1 Required. Specifies a DateTime object
  • datetime2 Required. Specifies a DateTime object
  • absolute Optional. Specifies a Boolean value. TRUE indicates that the interval/difference MUST be positive. Default is FALSE.

Return Value – This function returns the difference between the first DateTime object and the second DateTime object otherwise it returns false default.

Get Time Difference in PHP

Example1

	$date1 = date_create("2021-07-05");
$date2 = date_create("2021-10-10");
$diff = date_diff($date1, $date2);
echo $diff->format("%R%a days");
	

OutPut

	// +97 days
	

Time Diff PHP

Example2

	<?php

$date1 = date_create('2021-07-05');
$date2 = date_create('2021-09-10');

$interval = date_diff($date1, $date2);

$minutes = $interval->days * 24 * 60;
$minutes += $interval->h * 60;
$minutes += $interval->i;

echo $minutes.' minutes';
?>
	

OutPut

	// 96480 minutes