How to calculate date difference between two dates using PHP
To calculate difference between two dates is not a difficult job for this you must be aware of date functions. But for calculating difference there is only one function used that is strtotime it will convert a date and time string to timestamp that can be used for further calculations .
<?php $date_from = '2012-12-20 00:00:00'; // date from which you want to calculate the difference $date_to = '2013-02-24 00:00:00'; // date to which you want to calculate the difference $timeinseconds_from = strtotime($date_from); // It will give time in seconds from $date_from $timeinseconds_to = strtotime($date_to); // It will give time in seconds from $date_to $differenc_in_seconds = $timeinseconds_to - $timeinseconds_from; // It will give difference in seconds from two dates $differnce_days=$differenc_in_seconds/3600/24; // it will give difference in days ?> <div> Time Difference in Seconds : <?php echo $differenc_in_seconds; ?> Time Difference in days : <?php echo $differnce_days; ?> </div>
Here you can see the calculations between two dates . The formula used here is seconds/seconds*minutes/hours so we got this output:
By using different different formulas you can calculate days ,months ,years or any required difference between two dates.