If I have two dates returned from an SQL query, how can I calculate the number of days they cover?
IE:
$date1 = '2013-08-08';
$date2 = '2013-08-12';
$days = ???;
If I have two dates returned from an SQL query, how can I calculate the number of days they cover?
IE:
$date1 = '2013-08-08';
$date2 = '2013-08-12';
$days = ???;
As an option you can return date difference right from your sql query along with other data using DATEDIFF()
SELECT date1,
date2,
DATEDIFF(date2, date1) date_diff
FROM ...
Sample output:
+------------+------------+-----------+ | date1 | date2 | date_diff | +------------+------------+-----------+ | 2013-08-08 | 2013-08-12 | 4 | +------------+------------+-----------+
Here is SQLFiddle demo
A purely php solution:
$date1= new DateTime("2013-08-08");
$date2 = new DateTime("2013-08-12");
$diff = $date1->diff($date2);
echo $diff->format("%a days ago");