How to get difference between two dates in PHP
When working with data in system development we consider the date’s factor when storing the data in the database or when submitting it for backend processing. Dates forms an integral part while analyzing data since they help us understand when the data was first published and how old is the data in the system, also it helps us know the order of when the data was added to a system. The challenge comes when now we have a date in the system and you want to countercheck with another date provided, where it involves getting the difference between these two dates.
In this article, we shall show you how to get the difference between two days and display your result in any format you may think of, we shall work with data in the database and also data in a sting format.
Dates in PHP are represented in different formats and different symbols represent various elements.
d – day of the month (eg. 10)
D – day of the week (eg. Thu)
l - full text for the day of the week (eg Thursday)
m - month of the year in numbers (eg 10)
M – textual representation of month of the year (eg. Oct)
y – two digits representation of the year (eg 21)
Y – four digits representation of the year (eg 2021)
h – hour representation in 12 hour format (eg 01)
H – hour representation in 24 hour format (eg 19)
i – minutes representation (eg 45)
s – seconds representation (eg 30)
The above symbols are the most common ones while working with dates but there are so many others that we have not mentioned in this article.
Below shows various date representations that you will come across
Remember when working with dates, you have to include the date() function which will be the only way that PHP will know you are referring to dates.
date("d-m-y") - result will be 15-10-21
date("d-m-Y") - result will be 15-10-2021
date("d-M-y") - result will be 15-Oct-21
date("D d-m-y") - result will be Fri 15-10-21
date("d-m-y h:i") - result will be 15-10-21 12:50
date("d-m-y h:i:s") - result will be 15-10-21 12:50:01
You can interchange the (-) sign with (/) sign but in every date structure you should maintain the sign used.
date("d/m/y h:i:s") - result will be 15/10/21 12:50:01
date("d/m/Y H:i:s") - result will be 15/10/2021 00:50:01
Calculate dates difference
To calculate the difference between two dates, for example,
We do not take the two dates and subtract, you will not the correct answer. We first convert the date into a UNIX timestamp which is basically the number of milliseconds counted from 01-01-1970. To convert the date to Unix timestamp we use the strtotime() function.
The result for the above conversion will be
// representing the number of seconds from 01-01-1970
To get the difference between the dates we have outlined above, we will follow the code snippet below
echo strtotime($date1)- strtotime($date2);
The result for the above will be
Meaning that the result is displayed back as a UNIX timestamp, to get the result in either days, months, years or in hours or minutes, you will need to do the following
Since we know the result is in seconds,
The abs() function is used to return a positive number while the round function rounds the number to the nearest whole number.
To get the difference between dates in minutes
We divide the result with 60000 since 1 minute = 60 s
echo abs(round(3024000/ 60));
the result will be 50400 minutes
To get the difference between dates in hours
We divide the result with 3600 since 1 hour = 60*60 s
echo abs(round(3024000/ 3600));
the result will be 840 hours
To get the difference between dates in days
We divide the result with 86400since 1 day = 24*60*60 s
echo abs(round(3024000/ 86400));
the result will be 35 hours
Please note this method works well for PHP versions that are 5.3 and below. In the next article we shall focus on the method that works for PHP versions that are higher.
Hope you have learned something on how to calculate the difference between two dates in PHP.