Home PHP Calculating the difference in days between 2 dates in PHP?
 
 
 

Calculating the difference in days between 2 dates in PHP?

PDF Print E-mail

Question: How to calculate the difference in days between 2 dates in PHP?

 

Answer:

 
 
$startDate = '1999-03-12';
 
$endDate = '2011-03-12';
 
 
 
$days = (strtotime($endDate)-strtotime($startDate)) / (60 * 60 * 24);
 
echo $days; //Will output 4383
 
 

In the above example we have a startdate and a enddate. We need to find out how many days are between the startdate and the enddate.

 

We convert both dates to a UNIX Timestamp using the strtotime PHP function.

 

After the conversion we substract the startdate from the endate and then divide it by the number of seconds in a day (60*60*24).

We then output the day difference between 2 dates by using the echo PHP command.

+/-
+/- 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!