PHP

Calculating the difference in days between 2 dates in PHP

Written by Ian Carnaghan · 20 sec read >
$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.

Last Updated On March 22, 2018
Written by Ian Carnaghan
I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment. In my spare time I teach undergraduate classes in web development. Profile

Leave a Reply

avatar
  Subscribe  
Notify of