How to subtract two dates in PHP
  John Mwaniki /   06 Sep 2021

How to subtract two dates in PHP

You can easily find the time difference between two dates using PHP.

Below are 2 different methods you can use.

Method 1: Subtracting Unix timestamps

Unix timestamp is the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC.

You can easily convert any date, in any format to a Unix timestamp using the in-built PHP function strtotime().

When you pass a date value in the strtotime() function, it returns its Unix timestamp as in the example below:

<?php
$date = "2021-09-06";
$timestamp = strtotime($date);

echo "The Unix timestamp for $date is $timestamp";

Now you can find the time difference in seconds between two dates as shown below:

<?php
$date1 = "2021-01-25";
$date2 = "2021-09-06";
$timestamp1 = strtotime($date1);
$timestamp2 = strtotime($date2);
$difference = $timestamp2 - $timestamp1;

echo "The time difference is $difference seconds";

Now that you have the time difference in seconds, you can easily convert it to other durations of your preference such as hours, days, months, years, etc.

Example

<?php
$date1 = "2021-01-25";
$date2 = "2021-09-06";
$difference = strtotime($date2)-strtotime($date1);

//Displaying time difference in minutes
echo "Difference is: ".$difference/60." hours
";

//Displaying time difference in hours
echo "Difference is: ".$difference/(60*60)." hours
";

//Displaying time difference in days
echo "Difference is: ".$difference/(24*60*60)." days";

To avoid getting a time difference with decimal points, use the floor() function to round down as in the example below.

<?php
$date1 = "2021-01-25 10:15:23";
$date2 = "2021-09-06 13:19:00";
$difference = strtotime($date2)-strtotime($date1);
echo "Difference is: ".floor($difference/(24*60*60))." days";

 

Method 2: Using date_diff() function

The date_diff() is an in-built PHP function used to get the difference between two dates.

It returns a DateInterval object on success or false on failure.

Syntax


date_diff($datetime1, $datetime2)

The date_diff() function accepts two DateTime objects of the two dates being subtracted.

To convert a date to a DateTime object, pass it to the date_create() or the DateTime object as shown below:

<?php
$datetime1 = date_create("2021-09-06");
// Or
$datetime1 = new DateTime("2021-09-06");

You then need to specify the time difference format that should be returned by the date_diff() by using the object operator (->) and the format() function with your preferred format.

Example

<?php
$datetime1 = date_create("2021-09-06");
$datetime2 = date_create("2021-01-25");

$diff = date_diff($datetime1, $datetime2);
$days = $diff->format("%a");
echo "The time difference is $days days";

From the above example, "%a" specifies the different format to days.

The table below shows the list of values that you can pass to the format() function:

Format Description
%y Years
%m Months
%d Days
%a Days
%h Hours
%i Minutes
%s Seconds

Example

<?php
$datetime1 = date_create("2021-09-06 10:00:20");
$datetime2 = date_create("2021-01-25 10:00:20");
$diff = date_diff($datetime1, $datetime2);

echo $diff->format("%m")."months<br>";
//Outputs difference in months.

echo $diff->format("%a")."days<br>";
//Outputs difference in days.

echo $diff->format("%h")."hours<br>";
//Outputs difference in hours.

echo $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");
//Outputs difference in years, months, days hours, minutes and seconds

The above code is the procedural way of doing it. The example below shows how to do it in the object-oriented way.

<?php
$datetime1 = new DateTime("2021-09-06 10:00:20");
$datetime2 = new DateTime("2021-01-25 10:00:20");
$diff = $datetime1->diff($datetime2);

echo $diff->format("%m")."months<br>";
//Outputs difference in months.

echo $diff->format("%a")."days<br>";
//Outputs difference in days.

echo $diff->format("%h")."hours<br>";
//Outputs difference in hours.

echo $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");
//Outputs difference in years, months, days hours, minutes and seconds

If you would want to display the time difference returned either as positive(+) or negative (-), add %R immediately before the format as in the example below.

<?php
$datetime1 = date_create("2021-09-06");
$datetime2 = date_create("2021-01-25");
echo date_diff($datetime1, $datetime2)->format("%R%a")." days<br>";
//Output: -224days
echo date_diff($datetime2, $datetime1)->format("%R%a")." days";
//Output: +224days

Note: %a returns the difference in total number of days. Only use it when you want to get the number of days. If you want to include days with a larger duration such as years or months, use the "%d" instead to show the number of days instead.

Conclusion

In this article, we have covered everything that you need to know in working with PHP dates subtraction using different methods.

PS: If you found this article helpful, and you would like to get notified when we add more incredible content, subscribe to our email newsletter.