
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate the Difference Between Two Dates in PHP
What is PHP?
PHP (Hypertext Preprocessor) is a widely used server-side scripting language for web development. It allows developers to embed code within HTML files, enabling the creation of dynamic web pages and interactions with databases. PHP is known for its simplicity, versatility, and extensive integration capabilities with popular databases. It offers a broad range of extensions and has a large community of developers, ensuring ample resources and support.
Using date_diff() Function
In PHP, the date_diff() function is used to calculate the difference between two DateTime objects. It returns a DateInterval object representing the difference between the two dates.
Example
<?php // Creates DateTime objects $datetime1 = date_create('2017-05-29'); $datetime2 = date_create('2023-06-20'); // Calculates the difference between DateTime objects $interval = date_diff($datetime1, $datetime2); // Printing result in years & months format echo $interval->format('%R%y years %m months'); ?>
Output
+6 years 0 months
Using date-time mathematical formula
In this example, we are using the date-time mathematical formula to calculate the difference between the dates that will be returned in years, months, and days, hours, minutes, and seconds.
Example
<?php // Declare and define two dates $date1 = strtotime("2020-06-01 18:36:20"); $date2 = strtotime("2023-11-23 8:25:35"); // Formulate the Difference between two dates $diff = abs($date2 - $date1); // To get the year divide the resultant date into // total seconds in a year (365*60*60*24) $years = floor($diff / (365*60*60*24)); // To get the month, subtract it with years and // divide the resultant date into // total seconds in a month (30*60*60*24) $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); // To get the day, subtract it with years and // months and divide the resultant date into // total seconds in a days (60*60*24) $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); // To get the hour, subtract it with years, // months & seconds and divide the resultant // date into total seconds in a hours (60*60) $hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24) / (60*60)); // To get the minutes, subtract it with years, // months, seconds and hours and divide the // resultant date into total seconds i.e. 60 $minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60); // To get the minutes, subtract it with years, // months, seconds, hours and minutes $seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60)); // Print the result printf("%d years, %d months, %d days, %d hours, " . "%d minutes, %d seconds", $years, $months, $days, $hours, $minutes, $seconds); ?>
Output
3 years, 5 months, 24 days, 14 hours, 49 minutes, 15 seconds
Using the mathematical formula approach
Example
<?php // Declare two dates $start_date = strtotime("2016-02-28"); $end_date = strtotime("2023-06-19"); // Get the difference and divide into // total no. seconds 60/60/24 to get // number of days echo "Difference between two dates: " . ($end_date - $start_date)/60/60/24; ?>
Output
Difference between two dates: 2668
Conclusion
To calculate the difference between two dates in PHP, you have a couple of options. One approach is to use the DateTime class and its date_diff() method to obtain the difference in days, considering leap years and different month lengths accurately. Another option is to convert the dates to Unix timestamps using strtotime(), subtract one timestamp from the other, and divide by the number of seconds in a day. This simpler mathematical formula approach provides the difference in whole days. Choose the method that suits your requirements and use the respective functions and calculations to get the desired result.