php

Calculate the Date Difference in PHP

The difference between the two dates can be calculated in multiple ways by using a PHP script. The date value can be generated by the date() function and the DateTime class of PHP. The date difference can be calculated by using the built-in function of PHP or without any built-in function. The date_diff() function is used to calculate the difference between two dates when the date values of the same format are created by using the date_create() function. The diff() method is used to calculate the difference between two dates, which are created by using the DateTime class. Different ways of calculating the date difference in PHP are provided in this tutorial.

Calculate the Date Difference Based on Timestamp Values

The strtotime() function returns the timestamp value of the date provided in this function’s first argument. The way of calculating the difference between the dates has been shown in this part of the tutorial:

Example 1: Print the Difference Between Two dates Based on the Timestamp
Create a PHP file with the following script to calculate the difference between the current date and a particular date. The strtotime() function has been used in the script to generate the timestamp values of both dates, and the subtraction value of these timestamp values has been converted into days.

<?php

//Set the current date
$today = date('Y-m-d');

//Assign a date in 'Y-m-d' format
$futureDate = "2022-09-30";

//Calculate the date difference based on timestamp values
$difference = strtotime($futureDate) - strtotime($today);

//Calculate difference in days
$days = abs($difference/(60 * 60)/24);

//Print the date difference in days
echo "<br/><h3>The difference between ".$today." and ".$futureDate." is ".$days." days.</h3>";
?>

The following output will appear after executing the previous script:

Calculate the Date Difference by Using date_diff() Function

The date_diff() is a built-in function of PHP to calculate the difference between two dates. The syntax of this function is provided below:

Syntax

date_diff($objDate1, $objDate2);

This function takes two date objects as the mandatory argument values, and it returns the difference between these date objects on success or False on failure. The use of this function has been shown below.

Example 2: Print the Difference Between Two Dates by Using the date_diff() Function
Create a PHP file with the following script to calculate the difference between the current date and a particular date which will be created by using the date_create() function. The date_diff() function has been used in the script to calculate the difference between two date objects. Next, the format() method was used to print the difference with the formatting. The positive value will be returned by the date_diff() function when the first date is greater than the second date. Otherwise, the negative value will be returned.

<?php

//Create the first date object that will assign the current date
$dateVal1 = date_create();
//Create the second date object that will assign a particular date
$dateVal2 = date_create('29 Sep 2022');

//Calculate the interval from the first date to the second date
$ival = date_diff($dateVal2, $dateVal1);

//Print the output
echo "The difference of <b>".$dateVal1->format('d-M-Y')."-".$dateVal2->format('d-M-Y') . "</b> is <b>". $ival->format('%r%a days')."</b><br/>";

//Calculate the interval from the second date to the first date
$ival = date_diff($dateVal1, $dateVal2);

//Print the output
echo "The difference of <b>".$dateVal2->format('d-M-Y')."-".$dateVal1->format('d-M-Y') . "</b> is <b>". $ival->format('%r%a days')."</b><br/>";

?>

The following output will appear after executing the previous script:

Calculate the Date Difference Using the DateTime Class

The diff() method of the DateTime class is another way of calculating the difference between two date objects. The use of this method has been shown here.

Example 3: Print the Difference Between Two Dates by Using the format() Method of the DateTime Class
Create a PHP file with the following script that will calculate the difference between two dates which will be created by using the DateTime class. The diff() method of this class has been used in the script to calculate the difference between two date objects. This class’s format() method has been used to print the difference between the dates with the formatting.

<?php

//Create the first date object
$dateVal1 = new DateTime('30 Dec 2022');

//Create the second date object
$dateVal2 = new DateTime('2022-10-30');

//Calculate the difference
$ival = $dateVal2->diff($dateVal1);

//Print the difference value
echo "<br/> <h3>The date difference is ". $ival->format('%y years.').", ".$ival->format('%m months').", and ".$ival->format('%d days.'). "</h3>";
?>

The following output will appear after executing the previous script:

Example 4: Print the Difference Between Two Dates Without Using the format() Method of the DateTime Class
Create a PHP file with the following script that will calculate the difference between two dates which will be created by using the DateTime class. The diff() method of this class has been used in the script to calculate the difference between two date objects. The formatted year, month, and day values will be printed using the properties of the interval object named $ival.

<?php

//Create the first date object
$dateVal1 = new DateTime('5 Nov 2022');

//Create the second date object
$dateVal2 = new DateTime('2022-10-31');

//Calculate the difference
$ival = $dateVal2->diff($dateVal1);

//Print the difference value
echo "<br/> <h3>The date difference is ". $ival->y.' years, '.$ival->m.' months, '.$ival->d.' days'. "</h3>";

?>

The following output will appear after executing the previous script:

Example 5: Calculate the Age Based on the Birthdate and Current Date
Create a PHP file with the following script to calculate a person’s age based on the current date and birthdate using the DateTime class and the diff() method:

<?php

//Create the first date object
$birthdate = new DateTime('16 Dec 2006');

//Create the second date object
$currentDate = new DateTime();

//Calculate the difference
$ival = $birthdate->diff($currentDate);

//Print the birthdate
echo "<h2>Your date of birth is ". $birthdate->format('d M, Y').".</h2>";

//Print the difference value
echo "<h3>Your age is now, ". $ival->y.' years, '.$ival->m.' months, '.$ival->d.' days.'. "</h3>";

?>

The following output will appear after executing the previous script:

Conclusion

The difference between the two dates is required for solving different programming problems. Different ways of calculating the difference between two dates have been shown in this tutorial by using multiple examples. I hope this tutorial will help PHP users to find the difference between two dates properly after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.