I’m trying to compare two dates in PHP and figure out how many days apart the dates are. I am grabbing the first date from a MySQL database and the second date is the current date. The date in the MySQL database looks like this “2010-01-21 15:21:46”
$date1 = date("Y-m-d", $product['hold_date'])
$date2 = date("Y-m-d");
How can I compare these two dates and simply return the amount of days?
EDIT ———————————————————————————
This works:
$date1 = date(strtotime('2009-12-25 15:21:46'));
$date2 = time();
$secondsDifference = $date2 - $date1;
$days = floor($secondsDifference / 86400);
echo $days;
This is the seconds between date2 and date 1. You may need to reorder depending upon which one is more recent/futher in the future. This is of course if you want a positive number.
Or you can use:
You can do simple arithmetic to find out the number of days
In order to get the MySQL date into a seconds format, use strtotime()
So do this: