Calculating the date difference in days using PHP

No Comments

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));

Image Credits: Photo by Isis França on Unsplash.

Last Updated On March 22, 2018

Related Articles

No results found

Leave a Reply

avatar
  Subscribe  
Notify of
Menu
Copy link