{"id":82,"date":"2019-09-16T14:53:45","date_gmt":"2019-09-16T14:53:45","guid":{"rendered":"https:\/\/tutorialio.com\/?p=82"},"modified":"2025-11-04T10:32:00","modified_gmt":"2025-11-04T10:32:00","slug":"calculate-the-difference-between-two-dates-in-php","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/calculate-the-difference-between-two-dates-in-php\/","title":{"rendered":"How Can I Calculate the Difference Between Two Dates in PHP?"},"content":{"rendered":"<p>Calculating the difference between two dates is not as easy as it sounds. There are a lot of things that can go wrong if you are not careful. For example, you have to take both leap years and daylight saving time into consideration. Simply converting two dates to a timestamp and subtracting the values is not reliable in all cases.<\/p>\n<p>Fortunately, the PHP <a href=\"http:\/\/php.net\/manual\/en\/book.datetime.php\">DateTime<\/a> class takes care of all this for us. In this tutorial, we will use the different methods from this class to get the difference between two dates.<\/p>\n<h2 id=\"get-the-difference-between-two-dates-using-datetimediff\">Get the Difference Between Two Dates Using DateTime::diff()<\/h2>\n<p>The <code>DateTime<\/code> class has a <a href=\"http:\/\/php.net\/manual\/en\/datetime.diff.php\">diff()<\/a> method which calculates the difference between two <code>DateTime<\/code> objects. It returns a <code>DateInterval<\/code> object which contains information like the number of years, months and days etc. between two given dates.<\/p>\n<p>Once you have the <code>DateInterval<\/code>, you can use the <a href=\"http:\/\/php.net\/manual\/en\/dateinterval.format.php\">format()<\/a> method to format it according to your needs. This method accepts different characters as part of its string parameter. Just remember that all these characters must be prefixed by a percent sign (%).<\/p>\n<p>Here is a list of all the format characters and their usage:<\/p>\n<ul>\n<li><strong>%<\/strong> \u2014 This will output a literal % character.<\/li>\n<li><strong>Y<\/strong> \u2014 This will output years numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 03, 15.<\/li>\n<li><strong>y<\/strong> \u2014 This will output years numerically without any leading zeroes. Examples are 1, 3, 15.<\/li>\n<li><strong>M<\/strong> \u2014 This will output months numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 05, 28.<\/li>\n<li><strong>m<\/strong> \u2014 This will output months numerically without any leading zeroes. Examples are 1, 3, 12.<\/li>\n<li><strong>D<\/strong> \u2014 This will output days numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 09, 29.<\/li>\n<li><strong>d<\/strong> \u2014 This will output days numerically without any leading zeroes. Examples are 1, 9, 31.<\/li>\n<li><strong>a<\/strong> \u2014 This will output the total number of days computed by <code>DateTime::diff()<\/code> or (unknown) otherwise.<\/li>\n<li><strong>H<\/strong> \u2014 This will output hours numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 23.<\/li>\n<li><strong>h<\/strong> \u2014 This will output hours numerically without any leading zeroes. Examples are 1, 23.<\/li>\n<li><strong>I<\/strong> \u2014 This will output minutes numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 03, 59.<\/li>\n<li><strong>i<\/strong> \u2014 This will output minutes numerically without any leading zeroes. Examples are 1, 3, 59.<\/li>\n<li><strong>S<\/strong> \u2014 This will output seconds numerically with at least 2 digits. Single digits will be followed by leading zero. Examples are 01, 03, 57.<\/li>\n<li><strong>s<\/strong> \u2014 This will output seconds numerically without any leading zeroes. Examples are 1, 3, 57.<\/li>\n<li><strong>F<\/strong> \u2014 This will output microseconds numerically with at least 6 digits. Single digits will be followed by leading zero. Examples are 007701, 052738, 428291.<\/li>\n<li><strong>f<\/strong> \u2014 This will output microseconds numerically with at least 6 digits. Single digits will be followed by leading zero. Examples are 7701, 52738, 428291.<\/li>\n<li><strong>R<\/strong> \u2014 This will output \u201c-\u201d when negative and \u201c+\u201d when positive.<\/li>\n<li><strong>r<\/strong> \u2014 This will output \u201c-\u201d when negative and nothing when positive.<\/li>\n<\/ul>\n<p>In the following example, we have calculated the number of years, months and days between two dates.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$first_date = new DateTime('1988-11-09');\n$second_date = new DateTime('2023-01-18');\n$interval = $first_date-&gt;diff($second_date);\n\n\/\/ Output \u2014 34 years 02 months and 09 days 00 hours 00 minutes and 00 seconds.\necho $interval-&gt;format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');\n\n$first_date = new DateTime('2010-07-19T10:56:37+00:00');\n$second_date = new DateTime('2018-09-12T23:10:15+05:30');\n$interval = $first_date-&gt;diff($second_date);\n\n\/\/ Output \u2014 08 years 01 months and 24 days 06 hours 43 minutes and 38 seconds.\necho $interval-&gt;format('%Y years %M months and %D days %H hours %I minutes and %S seconds.');<\/code><\/pre>\n<p>The <code>DateTime<\/code> class takes care of everything for you like different number of days in different months, leap years and daylight saving time etc.<\/p>\n<h3 id=\"get-the-number-of-days-between-two-dates\">Get the Number of Days Between Two Dates<\/h3>\n<p>Once you have computed the difference between two dates using the <code>diff()<\/code> method, getting the number of days between them is easy. You just have to pass the <code>%a<\/code> character sequence to the <code>format()<\/code> method. It is important the remember that the days returned by <code>%D<\/code> or <code>%d<\/code> and <code>%a<\/code> are different.<\/p>\n<p>The <code>%D<\/code> and <code>%d<\/code> character sequences return the number of days between two dates after subtracting the number of months and years. Their value will never be greater than 31. On the other hand, the <code>%a<\/code> character sequence returns the total number of days that have passed between two dates. Here is an example:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$first_date = new DateTime('1988-11-09');\n$second_date = new DateTime('2023-05-09');\n$interval = $first_date-&gt;diff($second_date);\n\n\/\/ Output \u2014 34 years 06 months and 00 days.\necho $interval-&gt;format('%Y years %M months and %D days.');\n\n\/\/ Output \u2014 Total number of days passed: 12599.\necho $interval-&gt;format('Total number of days passed: %a.');<\/code><\/pre>\n<h2 id=\"calculate-the-number-of-hours-between-two-dates\">Calculate the Number of Hours Between Two Dates<\/h2>\n<p>While the format method has the <code>%a<\/code> character sequence to get the total number of days that have passed between two dates, there is no such character to get the total number of hours. However, we can do that calculation ourselves.<\/p>\n<p>So far, we have only created <code>DateTime<\/code> objects where we did not pass any information about the time of the day in the string. This meant that value returned for <code>%H<\/code> (hours), <code>%I<\/code> (minutes) and <code>%S<\/code> (seconds) would always be zero. Since we want to learn how to calculate the number of hours between two dates, we will pass strings with different times and\/or dates this time. The good thing about the <code>diff()<\/code> method of the <code>DateTime()<\/code> class is that it also takes care of the difference in timezones.<\/p>\n<p>In the following example, the number of hours returned will be different because of different time zones.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$first_date = new DateTime('2018-01-09T11:56:59+00:00');\n$second_date = new DateTime('2018-01-09T17:19:29+00:00');\n$interval = $first_date-&gt;diff($second_date);\n\n\/\/ Output \u2014 (+) 05 hours 22 minutes and 30 seconds.\necho $interval-&gt;format('(%R) %H hours %I minutes and %S seconds.');\n\n$first_date = new DateTime('2018-01-09T11:56:59+00:00');\n$second_date = new DateTime('2018-01-09T17:19:29-08:00');\n$interval = $first_date-&gt;diff($second_date);\n\n\/\/ Output \u2014 (+) 13 hours 22 minutes and 30 seconds.\necho $interval-&gt;format('(%R) %H hours %I minutes and %S seconds.');\n\n$first_date = new DateTime('2018-01-09T11:56:59+00:00');\n$second_date = new DateTime('2018-01-09T17:19:29+08:00');\n$interval = $first_date-&gt;diff($second_date);\n\n\/\/ Output \u2014 (-) 02 hours 37 minutes and 30 seconds.\necho $interval-&gt;format('(%R) %H hours %I minutes and %S seconds.');<\/code><\/pre>\n<p>The (+) sign implies that the second date is in future with respect to the first date. The (-) sign implies that the second date is in the past with respect to the first date.<\/p>\n<p>When the difference between two dates is less than a day, the value returned by <code>%H<\/code> will accurately give you the difference between them in hours. When the difference between two dates is more than a day like a week, some months or a couple of years etc, you can just multiply the number of days returned by <code>%a<\/code> with 24 and then add that to the number of hours returned by <code>%H<\/code>. Again, you don\u2019t have to worry about leap years etc. because the <code>DateTime<\/code> class takes care of all that for you.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$first_date = new DateTime('2018-01-19T11:56:59+00:00');\n$second_date = new DateTime('2018-01-09T17:19:29+08:00');\n$interval = $first_date-&gt;diff($second_date);\n\n$days_passed = $interval-&gt;format('%a');\n$hours_diff = $interval-&gt;format('%H');\n\n\/\/ Output \u2014 Total Days Passed: 10\necho 'Total Days Passed: '.$days_passed;\n\n\/\/ Output \u2014 Hour Difference in Two Times: 02\necho 'Hour Difference in Two Times: '.$hours_diff;\n\n\/\/ Output \u2014 Total Hours Passed: 242\necho 'Total Hours Passed: '.($days_passed*24 + $hours_diff);<\/code><\/pre>\n<p>You might have noticed that the second date in above example is in the past relative the the first date. The time in second date becomes 9:19:29 after accounting for the difference in timezone. That explain the 02 value returned by <code>%H<\/code>.<\/p>\n<p>In the following example, I have switched the dates. The timezone difference is still the same. Therefore, the time for second date stays 9:19:29. Since this is less than 11:56:59 in the first date, the number of days passed changes from 10 to 9. This is because the days are counted only up to 2018-01-18 11:56:59 as 2018-01-19 11:56:59 would be more than the second date (after accounting for timezone differences). Now, hours passed from 2018-01-18 11:56:59 to 2018-01-19 9:19:29 are 21. This takes our total number of hours to 237.<\/p>\n<p>As you can see, the <code>DateTime<\/code> class handles all the complications for you and gives you the correct result without any problem.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$first_date = new DateTime('2018-01-09T11:56:59+00:00');\n$second_date = new DateTime('2018-01-19T17:19:29+08:00');\n$interval = $first_date-&gt;diff($second_date);\n\n$days_passed = $interval-&gt;format('%a');\n$hours_passed = $interval-&gt;format('%H');\n\n\/\/ Output \u2014 Total Days Passed: 9\necho 'Total Days Passed: '.$days_passed;\n\n\/\/ Output \u2014 Hour Difference in Two Times: 21\necho 'Hour Difference in Two Times: '.$hours_passed;\n\n\/\/ Output \u2014 Total Hours Passed: 237\necho 'Total Hours Passed: '.($days_passed*24 + $hours_passed);<\/code><\/pre>\n<h2 id=\"calculate-the-number-of-minutes-between-two-dates\">Calculate the Number of Minutes Between Two Dates<\/h2>\n<p>The number of minutes returned by the <code>%I<\/code> character sequence in the <code>format()<\/code> method is equal to the number of minutes left after the <code>diff()<\/code> function had already accounted for all the days and hours that have passed. If you want to get the total number of minutes that have passed between two dates, you will have to do some calculations yourself.<\/p>\n<p>We first have to convert the number of days returned by <code>diff()<\/code> into hours by multiplying it with 24 and after that convert the total number of hours to minutes by multiplying the result with 60. The <code>DateTime<\/code> class automatically takes care of leap years and day time savings for you so simple multiplication gives accurate results.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$first_date = new DateTime('2018-01-19T11:56:59+00:00');\n$second_date = new DateTime('1993-10-09T17:34:22+08:00');\n$interval = $first_date-&gt;diff($second_date);\n\n$days_passed = $interval-&gt;format('%a');\n$hours_diff = $interval-&gt;format('%H');\n$minutes_diff = $interval-&gt;format('%I');\n$total_minutes = (($days_passed*24 + $hours_diff) * 60 + $minutes_diff);\n\n\/\/ Output \u2014 Total Days Passed: 8868\necho 'Total Days Passed: '.$days_passed;\n\n\/\/ Output \u2014 Hour Difference in Two Times: 02\necho 'Hour Difference in Two Times: '.$hours_diff;\n\n\/\/ Output \u2014 Minute Difference in Two Times: 22\necho 'Minute Difference in Two Times: '.$minutes_diff;\n\n\/\/ Output \u2014 Total Minutes Passed: 12770062\necho 'Total Minutes Passed: '.$total_minutes;<\/code><\/pre>\n<p>One thing that I would like to repeat is that you have to use <code>%I<\/code> to get the minutes. It is common to use <code>%M<\/code> in a hurry but remember that <code>%M<\/code> is reserved for months.<\/p>\n<h2 id=\"calculate-the-number-of-seconds-between-two-dates\">Calculate the Number of Seconds Between Two Dates<\/h2>\n<p>Just like minutes, the number of seconds returned by the <code>%S<\/code> character sequence in the <code>format()<\/code> method are calculated after the <code>diff()<\/code> method has already account for the number of days, hours and minutes that have already passed. However, calculating the total number of seconds that have passed between two dates is easy once we have the days, hours and minutes. All you have to do is convert them to seconds.<\/p>\n<p>We begin by converting the days and hours to minutes like we did in the previous section. After that, we can just multiply the result by 60 and add the number of seconds returned by the <code>%S<\/code> character sequence.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$first_date = new DateTime('2018-01-19T00:00:00+00:00');\n$second_date = new DateTime('1994-02-25T17:34:22+05:30');\n$interval = $first_date-&gt;diff($second_date);\n\n$days_passed = $interval-&gt;format('%a');\n$hours_diff = $interval-&gt;format('%H');\n$minutes_diff = $interval-&gt;format('%I');\n$seconds_diff = $interval-&gt;format('%S');\n$total_minutes = (($days_passed*24 + $hours_diff) * 60 + $minutes_diff);\n$total_seconds = $total_minutes*60 + $seconds_diff;\n\n\/\/ Output \u2014 Total Days Passed: 8728\necho 'Total Days Passed: '.$days_passed;\n\n\/\/ Output \u2014 Hour Difference in Two Times: 11\necho 'Hour Difference in Two Times: '.$hours_diff;\n\n\/\/ Output \u2014 Minute Difference in Two Times: 55\necho 'Minute Difference in Two Times: '.$minutes_diff;\n\n\/\/ Output \u2014 Seconds Difference in Two Times: 38\necho 'Seconds Difference in Two Times: '.$seconds_diff;\n\n\/\/ Output \u2014 Total Seconds Passed: 754142138\necho 'Total Seconds Passed: '.$total_seconds;<\/code><\/pre>\n<h2 id=\"quick-summary\">Quick Summary<\/h2>\n<p>Let&#8217;s recap everything that we have covered in this tutorial.<\/p>\n<ol id=\"summary-points\">\n<li>You can calculate the difference between two dates using the <code>diff()<\/code> method of the <code>DateTime<\/code> class. After that, you can echo the result in a specific format using the <code>format()<\/code> method.<\/li>\n<li>If you want to calculate the total number of hours between two dates, you can simply multiply the number of days returned by <code>diff()<\/code> with 24 and then add the result to the number of hours returned by <code>diff()<\/code>.<\/li>\n<li>Similarly, you can calculate the total number of minutes between two dates by first calculating the total hours passed and multiplying that by 60. After that, just add the result to the number of minutes returned by <code>diff()<\/code>.<\/li>\n<li>The same technique can be applied to calculate the number of seconds between two dates. Just calculate the number of minutes first and convert them to seconds by multiplying with 60. After that you just have to add the result to the number of seconds returned by <code>diff()<\/code>.<\/li>\n<\/ol>\n<p>Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to calculate the number of days, hours, minutes or seconds between two dates in PHP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Calculating the difference between two dates is not as easy as it sounds. There are a lot of things that can go wrong if you are not careful. For example, you have to take both leap years and daylight saving time into consideration. Simply converting two dates to a timestamp and subtracting the values is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-how-to-guides"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=82"}],"version-history":[{"count":1,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":10252,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/82\/revisions\/10252"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}