tags:

views:

99

answers:

7

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;
+3  A: 
$secondsDifference = $date2 - $date1;

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:

$secondsDifference = abs($date2 - $date1);

You can do simple arithmetic to find out the number of days

define("SECONDS_IN_DAY", 60*60*24);
$days = floor($secondsDifference / SECONDS_IN_DAY); // rounds down
// you can use ceil() to round it up.

In order to get the MySQL date into a seconds format, use strtotime()

$time = strtotime($mysql_time);

So do this:

$date1 = strtotime($product['hold_date']);
$date2 = time();
Chacha102
I thought the date function that is setting the values of $date1 and $date2 returned a string of the relevant format, rather than a number of seconds, so you can't subtract them from one another?
PimTerry
This is returning 0 for me, not sure why???
mike
I looked up the documentation (http://php.net/manual/en/function.date.php), which says it does return a string, which is almost certainly why you're getting 0 back; subtracting one string from another doesn't make any sense to PHP.
PimTerry
@PimTerry Updated to use the `time()` function.
Chacha102
This worked now!
mike
A: 
$secondsPerDay = 24 * 60 * 60;
$differenceInSeconds = $date2 - $date1;
$differenceInDays = $differenceInSeconds / $secondsPerDay;
echo
Use `floor()` and you win ;)
Franz
good point, as long as integers are desired.
echo
$date1 = date('Y-m-d', '2010-01-22 15:21:46');$date2 = date('Y-m-d');$secondsPerDay = 24 * 60 * 60;$differenceInSeconds = $date2 - $date1;$differenceInDays = $differenceInSeconds / $secondsPerDay;echo $differenceInDays;This returns 0.000474537037037
mike
That's because date() takes a timestamp (not a string) as its second argument.
echo
A: 

If you can convert the dates to timestamps, you can do it like this:

$numDays = floor(($timestamp2 - $timestamp1) / (24 * 60 * 60));
Franz
Why the downvote?
Franz
A: 

With PHP 5.3 look at date_diff()

johannes
A: 
$d1 = strtotime("2010-01-21 15:21:46");
$d2 = strtotime($product['hold_date']);
echo floor(($d2-$d1)/86400) ;
ghostdog74
+2  A: 

Strtotime will turn a date/time string into a unix timestamp, the number of seconds between the given time and the unix epoch (January 1, 1970). The time function will give you the unix timestamp for now.

You can then take one date away from the other to get the number of seconds apart the days are, and divide by 86400 (60x60x24) to get the number of days that have passed.

As an alternative to converting from mysql date time to unix inside PHP, you could use the UNIX_TIMESTAMP MySQL function.

Edit: As others have said, you probably want to floor() or ceil() the resulting number of days to get an integer.

PimTerry
+1  A: 

May I suggest that you use MySQL (when possible) to calculate the difference in dates? MySQL's date functions are far more superior than PHP's. Consider:

# with CURDATE() resolving to 2010-01-25, this returns -3
SELECT DATEDIFF( '2010-01-22 15:21:46', CURDATE() );

So, I imagine you are doing something like this now:

SELECT
    product_id,
    hold_date
FROM
    products
WHERE
    something = true

Simply alter it to:

SELECT
    product_id,
    hold_date,
    DATEDIFF( hold_date, CURDATE() ) as hold_date_diff_days
FROM
    products
WHERE
    something = true
fireeyedboy