Program to find the number of days between two dates in PHP
Last Updated :
30 Jul, 2024
In this article, we will see how to get the date difference in the number of days in PHP, along with will also understand the various ways to get the total count of difference in 2 dates & see their implementation through the examples. We have given two dates & our task is to find the number of days between these given dates. For this, we will be following the below 2 methods:
Consider the following example:
Input : date1 = "2-05-2017"
date2 = "25-12-2017"
Output: Difference between two dates: 237 Days
Explanation: Calculating the total number of days between the start & end date.
Note: The dates can be taken in any format. In the above example, the date is taken in dd-mm-yyyy format.
This is a built-in function in PHP that is used to convert an English textual date-time description to a UNIX timestamp. The function accepts a string parameter in English which represents the description of date-time. For e.g., “now” refers to the current date in the English date-time description. The function returns the time in seconds since the Unix Epoch
Example 1: In this example, we have taken two dates and calculated their differences.
PHP
<?php
// Function to find the difference
// between two dates.
function dateDiffInDays($date1, $date2) {
// Calculating the difference in timestamps
$diff = strtotime($date2) - strtotime($date1);
// 1 day = 24 hours
// 24 * 60 * 60 = 86400 seconds
return abs(round($diff / 86400));
}
// Start date
$date1 = "17-09-2018";
// End date
$date2 = "31-09-2018";
// Function call to find date difference
$dateDiff = dateDiffInDays($date1, $date2);
// Display the result
printf("Difference between two dates: "
. $dateDiff . " Days ");
?>
OutputDifference between two dates: 14 Days
The date_diff() function is an inbuilt function in PHP that is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure.
Example: This example describes calculating the number of days between the 2 dates in PHP.
PHP
<?php
// Creates DateTime objects
$datetime1 = date_create('17-09-2018');
$datetime2 = date_create('25-09-2018');
// Calculates the difference between DateTime objects
$interval = date_diff($datetime1, $datetime2);
// Display the result
echo $interval->format('Difference between two dates: %R%a days');
?>
OutputDifference between two dates: +8 days
Method 3: Using DateTime Class
The DateTime class in PHP offers a more object-oriented approach to handling dates and times. We can use the diff method of the DateTime class to calculate the difference between two dates. This method returns a DateInterval object, which represents the difference between the two dates.
Example: Calculating the Number of Days Between Two Dates
In this example, we will create DateTime objects for the two dates and use the diff method to calculate the difference.
PHP
<?php
function dateDiffUsingDateTime($date1, $date2) {
$datetime1 = new DateTime($date1);
$datetime2 = new DateTime($date2);
$interval = $datetime1->diff($datetime2);
return $interval->days;
}
$date1 = "17-09-2018";
$date2 = "25-12-2018";
$dateDiff = dateDiffUsingDateTime($date1, $date2);
printf("Difference between two dates: " . $dateDiff . " Days");
?>
OutputDifference between two dates: 99 Days
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.
Similar Reads
PHP | Number of week days between two dates You are given two string (dd-mm-yyyy) representing two date, you have to find number of all weekdays present in between given dates.(both inclusive) Examples: Input : startDate = "01-01-2018" endDate = "01-03-2018" Output : Array ( [Monday] => 9 [Tuesday] => 9 [Wednesday] => 9 [Thursday] =
2 min read
How to calculate number of days between two dates in R ? In this article, we will discuss how to Find the number of days between two dates in the R programming language. Working with Dates in RDates play a crucial role in data analysis, and figuring out the time difference between two dates is a typical job. Calculating the number of days between two dat
2 min read
How to Calculate the Number of Days between Two Dates in JavaScript? Calculating the number of days between two dates is a common task in web development, especially when working with schedules, deadlines, or event planning. JavaScript provides a simple and effective way to perform this calculation by using different approaches. Whether you're comparing past and futu
4 min read
How to Calculate the Number of Days Between two Dates in TypeScript ? In TypeScript, calculating the number of days between two dates is a common task in many applications. Whether you're working with date ranges, scheduling, or performing date-based calculations, knowing how to accurately determine the difference in days between two dates is essential. Different appr
3 min read
Return all dates between two dates in an array in PHP Returning all dates between two dates in an array means generating a list of all consecutive dates from the start date to the end date, inclusive, and storing each date as an element in an array for easy access.Here we have some common methods:Table of ContentUsing DatePeriod ClassUsing strtotime()
4 min read