Program to find the number of days between two dates in PHP
Given two dates. The task is to find the number of days between the given dates.
Examples:
Input : date1 = "17-09-2018"
date2 = "31-09-2018"
Output: Difference between two dates: 14 days
Input : date1 = "2-05-2017"
date2 = "25-12-2017"
Output: Difference between two dates: 237 Days
Note: The dates can be taken in any format. In the above example, the date is taken in dd-mm-yyyy format.
Method 1: In this method, first take two dates and get their differences. Below is the implementation of this method.
<?php // PHP code to find the number of days // between two given dates // Function to find the difference // between two dates. function dateDiffInDays($date1, $date2) { // Calulating 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 "); ?> |
Difference between two dates: 14 Days
Method 2 Using date_diff() Function: The date_diff() function is an inbuilt function in PHP which is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure.
Example:
<?php // PHP code to find the number of days // between two given dates // 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'); ?> |
Difference between two dates: +8 days
Recommended Posts:
- PHP | Number of week days between two dates
- How to calculate the number of days between two dates in javascript?
- How to store all dates in an array present in between given two dates in JavaScript ?
- Return all dates between two dates in an array in PHP
- How to get the number of days in a specified month using JavaScript ?
- Comparing two dates in PHP
- Compare Dates in Java
- How to calculate the difference between two dates in PHP?
- Compare two dates using JavaScript
- Sort an array of dates in PHP
- How to add days to $Date in PHP?
- How to select Min/Max dates in an array using JavaScript ?
- How to calculate minutes between two dates in JavaScript ?
- Subtract days from Date object in JavaScript
- JavaScript | Adding days in milliseconds to Date object
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



