+

# How do I calculate the difference between two dates in PHP?

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 the date_diff() function to find difference between two dates.Method 2:Use the date-time mathematical formula to find the difference between two dates. Returns years, months, days, hours, minutes, seconds between two specified dates.Program:```   // 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 the two dates \$diff = abs ( \$date2 - \$date1 );   // To get the year, divide the received date by // total number of seconds in a year (365 * 60 * 60 * 24) \$years = floor ( \$diff / (365 * 60 * 60 * 24));   // To get the month, subtract it over the years and // divide the resulting date by // total number of seconds in a month (30 * 60 * 60 * 24) \$months = floor (( \$diff - \$years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));   // To get a day, subtract it from years and // months and divide the resulting date by // total number of seconds in 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 over the years // months and seconds and divide the resulting value // date in total number of seconds in hours (60 * 60) \$hours = floor (( \$diff - \$years * 365 * 60 * 60 * 24 - \$months * 30 * 60 * 60 * 24 - \$days * 60 * 60 * 24) / (60 * 60));   // To get minutes, subtract this over the years // months, seconds and hours and divide // resulting date in total number of 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 minutes, subtract this over the 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 ); ?> Exit: 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.   // Declare two dates \$start_date = strtotime ( " 2018-06-08 " ); \$end_date = strtotime ( "2018-09-19" );   // Get the difference and divide by // nothing at all. 60/60/24 seconds to get // number of days echo "Difference between two dates:" ... ( \$end_date - \$start_date ) / 60/60/24; ?> Exit: Difference between two dates: 103 ```
``` ```
``` ```
``` ```
``` Get Solution for free from DataCamp guru © 2021 Python.Engineering Best Python tutorials books for beginners and professionals Python.Engineering is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com Computations Development Cryptography For dummies Machine Learning Big Data Loops Counters NumPy NLP PHP Regular Expressions File Handling Arrays String Variables Knowledge Database X Submit new EBook \$(document).ready(function () { \$(".modal_galery").owlCarousel({ items: 1, itemsCustom: false, itemsDesktop: [1300, 1], itemsDesktopSmall: [960, 1], itemsTablet: [768, 1], itemsTabletSmall: false, itemsMobile: [479, 1], singleItem: false, itemsScaleUp: false, pagination: false, navigation: true, rewindNav: true, autoPlay: true, stopOnHover: true, navigationText: [ "<img class='img_no_nav_mob' src='/wp-content/themes/nimani/image/prevCopy.png'>", "<img class='img_no_nav_mob' src='/wp-content/themes/nimani/image/nextCopy.png'>" ], }); \$(".tel_mask").mask("+9(999) 999-99-99"); }) ```