Given two dates, start_date and end_date. The task is to find the difference between two dates.
Examples:
Input: start_date: 2016-06-01 22:45:00
end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Input: start_date: 2015-05-12 12:37:30
end_date: 2018-04-25 12:45:31
Output: 2 years, 11 months, 19 days, 0 hours, 8 minutes, 1 seconds
Method 1: Use date_diff() Function to find the difference between two dates.
Method 2: To use date-time mathematical formula to find the difference between two dates. It returns the years, months, days, hours, minutes, seconds between two specified dates.
Program:
<?php // Declare and define two dates $date1 = strtotime("2016-06-01 22:45:00"); $date2 = strtotime("2018-09-21 10:44:01"); // Formulate the Difference between two dates $diff = abs($date2 - $date1); // To get the year divide the resultant date into // total seconds in a year (365*60*60*24) $years = floor($diff / (365*60*60*24)); // To get the month, subtract it with years and // divide the resultant date into // total seconds in a month (30*60*60*24) $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); // To get the day, subtract it with years and // months and divide the resultant date into // total seconds in a days (60*60*24) $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); // To get the hour, subtract it with years, // months & seconds and divide the resultant // date into total seconds in a hours (60*60) $hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24) / (60*60)); // To get the minutes, subtract it with years, // months, seconds and hours and divide the // resultant date into total seconds i.e. 60 $minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60); // To get the minutes, subtract it with years, // months, seconds, hours and minutes $seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60)); // Print the result printf("%d years, %d months, %d days, %d hours, " . "%d minutes, %d seconds", $years, $months, $days, $hours, $minutes, $seconds); ?> |
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Method 3: This method is used to get the total number of days between two specified dates.
<?php // Declare two dates $start_date = strtotime("2018-06-08"); $end_date = strtotime("2018-09-19"); // Get the difference and divide into // total no. seconds 60/60/24 to get // number of days echo "Difference between two dates: " . ($end_date - $start_date)/60/60/24; ?> |
Difference between two dates: 103
Recommended Posts:
- Return all dates between two dates in an array in PHP
- How to store all dates in an array present in between given two dates in JavaScript ?
- How to calculate minutes between two dates in JavaScript ?
- How to calculate the number of days between two dates in javascript?
- Program to find the number of days between two dates in PHP
- PHP | Number of week days between two dates
- Comparing two dates in PHP
- How to check if one date is between two dates in JavaScript ?
- Calculating total number of Hours, Days, Minutes and Seconds between two dates in Golang
- Get the relative timestamp difference between dates in JavaScript
- Compare two dates using JavaScript
- Sort an array of dates in PHP
- Compare Dates in Java
- How to select Min/Max dates in an array using JavaScript ?
- How to get highcharts dates in the x-axis ?
- How to calculate total time of an array in PHP ?
- How to run two PHP versions for two projects on same apache server ?
- How to calculate percent minus px in SASS ?
- How to calculate the yesterday's date in JavaScript ?
- How to calculate the date three months prior using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

