mike
mike

Reputation: 2760

Date difference in PHP?

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;

Upvotes: 2

Views: 1426

Answers (7)

Decent Dabbler
Decent Dabbler

Reputation: 22783

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

Upvotes: 1

Tyler Carter
Tyler Carter

Reputation: 61557

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

Upvotes: 4

Tim Perry
Tim Perry

Reputation: 13216

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.

Upvotes: 2

ghostdog74
ghostdog74

Reputation: 342283

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

Upvotes: 0

johannes
johannes

Reputation: 15969

With PHP 5.3 look at date_diff()

Upvotes: 0

Franz
Franz

Reputation: 11553

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

$numDays = floor(($timestamp2 - $timestamp1) / (24 * 60 * 60));

Upvotes: 0

echo
echo

Reputation: 7845

$secondsPerDay = 24 * 60 * 60;
$differenceInSeconds = $date2 - $date1;
$differenceInDays = $differenceInSeconds / $secondsPerDay;

Upvotes: 0

Related Questions