Home PHP Calculating the date difference in days using PHP
 
 
 

Calculating the date difference in days using PHP

PDF Print E-mail

If you have 2 dates and you need to calculate the total days between these two dates using PHP you will need to do the following

First we need to convert the two dates into UNIX timestamps in seconds

 
$date1 = strtotime('2010-10-12');
$date2 = strtotime('2011-11-12'); 
 

Calculate the difference between these two dates in seconds

$diff = $date2-$date1;

Get the total of Days

$days = floor($diff/(60*60*24));

The floor function is there to get complete days. 60*60*24 means that each day has 24 hours, each hour has 60 minutes and each minute has 60 seconds.

Complete example

 
$date1 = strtotime('2010-10-12');
$date2 = strtotime('2011-11-12');
 
$diff = $date2-$date1; 
 
$days = floor($diff/(60*60*24));

+/-
+/- Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
Security Please input the anti-spam code that you can read in the image.
 

Go Daddy $7.49 .com Sale!