How will you calculate dates between two dates in php

In this article, we will see how to calculate the difference between 2 dates in PHP, along with understanding its implementation through the examples. Given two dates ie., start_date and end_date & we need to find the difference between the two dates.

Consider the below example:

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
Explanation: The difference of 2 dates will give the date in complete format.

Method 1: Using date_diff() Function

This function is used to find the difference between two dates. This function will return a DateInterval object on the success and returns FALSE on failure.

Example: This example illustrates the use of the date_diff() function to calculate the difference between the 2 dates.

PHP

<?php

  $datetime1 = date_create('2016-06-01');

  $datetime2 = date_create('2018-09-21');

  $interval = date_diff($datetime1, $datetime2);

  echo $interval->format('%R%y years %m months');

?>

Output:

+2 years 3 months

Method 2: To use the date-time mathematical formula to find the difference between two dates. It returns the years, months, days, hours, minutes, seconds between two specified dates.

Example: In this example, we will be using the date-time mathematical formula to calculate the difference between the dates that will be returned in years, months, days, hours, minutes, seconds.

PHP

<?php

  $date1 = strtotime("2016-06-01 22:45:00");

  $date2 = strtotime("2018-09-21 10:44:01");

  $diff = abs($date2 - $date1);

  $years = floor($diff / (365*60*60*24));

  $months = floor(($diff - $years * 365*60*60*24)

                                 / (30*60*60*24));

  $days = floor(($diff - $years * 365*60*60*24 -

               $months*30*60*60*24)/ (60*60*24));

  $hours = floor(($diff - $years * 365*60*60*24

         - $months*30*60*60*24 - $days*60*60*24)

                                     / (60*60));

  $minutes = floor(($diff - $years * 365*60*60*24

           - $months*30*60*60*24 - $days*60*60*24

                            - $hours*60*60)/ 60);

  $seconds = floor(($diff - $years * 365*60*60*24

           - $months*30*60*60*24 - $days*60*60*24

                  - $hours*60*60 - $minutes*60));

  printf("%d years, %d months, %d days, %d hours, "

       . "%d minutes, %d seconds", $years, $months,

               $days, $hours, $minutes, $seconds);

?>

Output:

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

<?php

  $start_date = strtotime("2018-06-08");

  $end_date = strtotime("2018-09-19");

  echo "Difference between two dates: "

      . ($end_date - $start_date)/60/60/24;

?>

Output:

Difference between two dates: 103

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


How do you calculate days between two dates?

To calculate the number of days between two dates, you need to subtract the start date from the end date. If this crosses several years, you should calculate the number of full years. For the period left over, work out the number of months. For the leftover period, work out the number of days.

How can I compare two dates in PHP?

Once you have created your DateTime objects, you can also call the diff() method on one object and pass it the other date in order to calculate the difference between the dates. This will give you back a DateInterval object. $last = new DateTime( "25 Dec 2020" ); $now = new DateTime( "now" );

How can I calculate hours between two dates in PHP?

You can convert them to timestamps and go from there: $hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

How do I calculate the number of weeks between two dates in PHP?

php function week_between_two_dates($date1, $date2) { $first = DateTime::createFromFormat('m/d/Y', $date1); $second = DateTime::createFromFormat('m/d/Y', $date2); if($date1 > $date2) return week_between_two_dates($date2, $date1); return floor($first->diff($second)->days/7); } $dt1 = '1/1/2014'; $dt2 = '12/31/2014'; ...