# Calculate difference between 2 dates, times | PHP Code Examples

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

In this article, we will learn how to calculate the difference between two dates and time in PHP. We will also understand its implementation through the examples. Let’s explore different methods through which we will find the difference between 2 dates and times in PHP. In examples, we will give two dates, start_date and end_date, and we will find the difference between these two dates.

## Calculate the Difference Between 2 Dates in PHP Code Example

``````<?php

// Creates DateTime objects.
\$datetime1 = date_create('2015-07-01');
\$datetime2 = date_create('2022-11-12');

// Calculates the difference between DateTime objects.
\$interval = date_diff(\$datetime1, \$datetime2);

// Prints the result in years and months format.
echo \$interval->format('%R%y years %m months');``````

## Using date_diff() Function in PHP to Calculate the Difference between 2 Dates

In PHP, the date_diff() function is used to find the difference between two dates. This function returns a DateInterval object on success and returns FALSE on failure.
The following PHP example explains the use of the date_diff() function to find the difference between the 2 dates.

``````<?php

// Creates DateTime objects.
\$datetime1 = date_create('2015-07-01');
\$datetime2 = date_create('2022-11-12');

// Calculates the difference between DateTime objects.
\$interval = date_diff(\$datetime1, \$datetime2);

// Prints the result in years and months format.
echo \$interval->format('%R%y years %m months');``````

Output:

``+7 years 4 months``

We used the date_diff() function in the above code example, and we can see in the output it calculates the difference between two dates.

## Method 2: Using the Date-time Mathematical Formula in PHP

We can use the date-time mathematical formula to find the difference between two dates and time in PHP. It will return the years, months, days, hours, minutes, and seconds between two given dates.

In the following example, we use the date-time mathematical formula to find the difference between the two dates and times. In the output, it will return the dates in years, months, days, hours, minutes, and seconds.

``````<?php

// Input the dates and times.
\$date1 = strtotime("2015-07-01 18:30:00");
\$date2 = strtotime("2023-11-12 10:40:6");

// This formulates the difference.
\$difference = abs(\$date2 - \$date1);

// To get the years, we use this formula.
\$year = floor(\$difference / (365*60*60*24));

// To get the months, we use this formula.
\$month = floor(
(\$difference - \$year * 365*60*60*24)
/ (30*60*60*24)
);

// To get the days, we use this formula.
\$day = floor(
(\$difference - \$year * 365*60*60*24 - \$month*30*60*60*24)
/ (60*60*24)
);

// To get the hours, we use this formula.
\$hour = floor(
(\$difference - \$year * 365*60*60*24 - \$month*30*60*60*24 - \$day*60*60*24)
/ (60*60)
);

// To get the minutes, we use this formula.
\$min = floor((\$difference - \$year * 365*60*60*24
- \$month*30*60*60*24 - \$day*60*60*24
- \$hour*60*60)/ 60);

// To get the seconds, we use this formula.
\$sec = floor((\$difference - \$year * 365*60*60*24
- \$month*30*60*60*24 - \$day*60*60*24
- \$hour*60*60 - \$min*60));

// Now print the result.
printf("%d Years, %d Months, %d Days, %d Hours, "
. "%d Minutes, %d Seconds", \$year, \$month,
\$day, \$hour, \$min, \$sec);
?>
``````

Output:

``8 Years, 4 Months, 15 Days, 17 Hours, 10 Minutes, 6 Seconds``

As we can see in the output, the above code returns the difference between two specific dates and times.

Using these two methods, we can easily calculate the difference between two dates and times in PHP.

## Calculate the number of days between two dates in PHP?

In this section, we will explore different methods to get the number of days between two dates in PHP.

### Method 1: Using DateTime::diff

In this method, we will create two DateTime objects and then find the difference using the diff() function in PHP.
The DateTime::diff returns the difference between two DateTime objects in PHP. In the following example, we use diff() to get the days difference between two dates in PHP. In the output, it returns the total days difference between the two dates.

``````<?php
// Input the origin date.
\$origin = new DateTime('2022-12-15');

// Input the target date.
\$target = new DateTime('2023-1-25');

// Calculates the difference of days between dates.
\$interval = \$origin->diff(\$target);

//Print the result.
echo \$interval->format('%R%a days');
?>
``````

Output:

``+41 days``

### Method 2: Using date_diff() function

We can also use a built-in date_diff function of PHP Date/Time to find the difference between the two dates. The date_diff() function returns the difference between two DateTime objects. The date_create function is used to create the DateTime object.

In the following example, we use  date_diff to get the days difference between two dates in PHP. We also use the date_create function to create the DateTime objects. This function returns the total days difference between the two dates in the output.

``````<?php
// Input and create the origin date.
\$origin = date_create('2022-11-15');

// Input and create the target date.
\$target = date_create('2023-1-25');

// Calculates the difference of days between dates.
\$interval = date_diff(\$origin, \$target);

//Print the result.
echo \$interval->format('%R%a days');
?>
``````

Output:

``+71 days``

### Method 3: Using strtotime() function

We recommended the first two methods to get the accurate difference between the two dates in PHP.

We can also use the strtotime() function to get the number of days between two dates. In the following example, we use the strtotime() function, and it returns the number of days between two specific dates.

``````<?php

//Input the startDate
\$start_date = strtotime("2022-12-08");

//Input the endtDate.
\$end_date = strtotime("2023-1-20");

//Get the difference of the days.
echo "The Difference of days between two dates: "
. (\$end_date - \$start_date)/60/60/24;
?>
``````

Output:

``````The Difference between two dates: 43
``````

## Calculate the Number of Hours Between Two Dates

Using the diff() function, we can calculate the number of hours between two dates in PHP. In the following example, we will create DateTime objects. Then we will pass strings with different times and dates to DateTime objects. The good thing about the diff() method of the DateTime() class is that it also takes care of the difference in time zones.

``````<?php
// Input the first date.
\$first_date = new DateTime('2023-01-09T9:45:59+00:00');

// Input the second date.
\$second_date = new DateTime('2023-01-09T17:30:29+00:00');

// Calculates the number of hours.
\$interval = \$first_date->diff(\$second_date);

//Print the result.
echo \$interval->format("The number of Hours between two dates are = ". '%H hours %I minutes and %S seconds.');

?>
``````

Output:

``The number of Hours between two dates are = 07 hours 44 minutes and 30 seconds.``

The above code returns the number of hours between two specific dates along with minutes and seconds.

## Calculate the Number of Minutes Between Two Dates

Using the diff() function, we can calculate the number of minutes between two dates in PHP. In the following example, we will create DateTime objects. We will have to do some calculations to get the total number of minutes between the two dates.

We first have to convert the number of days returned by diff() into hours by multiplying it by 24. And after that, we will convert the total number of hours to minutes by multiplying the result by 60. The DateTime class automatically takes care of leap years, and daytime savings for you, so simple multiplication gives accurate results.

``````<?php

// Input the first date.
\$first_date = new DateTime('2023-01-20T11:40:59+00:00');

// Input the second date.
\$second_date = new DateTime('2023-2-09T17:34:22+08:00');

\$interval = \$first_date->diff(\$second_date);
\$days_passed = \$interval->format('%a');
\$hours_diff = \$interval->format('%H');
\$minutes_diff = \$interval->format('%I');

//Converts days into minutes.
\$total_minutes = ((\$days_passed*24 + \$hours_diff) * 60 + \$minutes_diff);

//Print the result.
echo 'The Total Minutes between two dates are: '.\$total_minutes;

?>
``````

Output:

``````The Total Minutes between two dates are: 28673
``````

## Calculate the Number of Seconds Between Two Dates in PHP

Using the diff() function, we can calculate the number of seconds between two dates in PHP. In the following example, first, we will create DateTime objects. And then, we need to convert the days and hours to minutes, as we did in the previous section. After that, we need to multiply the result by 60 and will add the number of seconds returned by the %S character sequence.

``````<?php

// Input the first date.
\$first_date = new DateTime('2023-01-20T11:40:59+00:00');

// Input the second date.
\$second_date = new DateTime('2023-2-09T17:34:22+08:00');

\$interval = \$first_date->diff(\$second_date);
\$days_passed = \$interval->format('%a');
\$hours_diff = \$interval->format('%H');
\$minutes_diff = \$interval->format('%I');
\$seconds_diff = \$interval->format('%S');

//Converts days into minutes.
\$total_minutes = ((\$days_passed*24 + \$hours_diff) * 60 + \$minutes_diff);

//Converts minutes into seconds.
\$total_seconds = \$total_minutes*60 + \$seconds_diff;

// Output — Total Seconds Passed: 754142138
echo 'The total number of seconds in two dates are: '.\$total_seconds;

?>
``````

Output:

``The total number of seconds in two dates are: 1720403``

## Calculate the difference between two dates and times in PHP Explored

We can calculate the difference between two dates using the diff() method in PHP. And if you want to calculate the total number of days between two dates, you can use the DateTime::diff method or date_diff() function or strtotime() function for that.

This article explains the different methods used for finding the difference between two dates and times in PHP. It also describes calculating the total number of hours, minutes and seconds between two dates in PHP.

## Learn the Fundamentals of Good Web Development

Please take a moment and sign up for our free email course on the fundamentals of good web development. Every week is packed with a roundup of articles on our site and from around the web, where we go deep into developing exceptional web applications. We have meetups, code reviews, slack chats, and more.