tags:

views:

50

answers:

1

This one works for dates in format: 0000-00-00

But I need a function for unix timestamp format

function dateDiff($start, $end) {

    $start_ts = strtotime($start);

    $end_ts = strtotime($end);

    $diff = $end_ts - $start_ts;

    return round($diff / 86400);

}

Could anyone help a brother out??

+3  A: 

Instead of calculating the timestamps from the two dates received as parameters, you could just receive those timestamps as parameters directly ; ie, just remove the calls to strtotime.

Something like this should do the trick, I suppose :

function dateDiffTs($start_ts, $end_ts) {
    $diff = $end_ts - $start_ts;
    return round($diff / 86400);
}

Afterall, strotime gets you the timestamp corresponding to a date ;-)


For instance, this :

var_dump(dateDiffTs(1251151200, 1251410400));

get you 3 days (1251151200 is 2009-08-25, and 1251410400 is 2009-08-28).

And this works if you have hours in the timestamps too :

var_dump(dateDiffTs(1251196200, 1251443700));

Gets you 3 days too (1251196200 is 2009-08-25 12:30:00 and 1251443700 is 2009-08-28 09:15:00).

Pascal MARTIN
thanks homie. that was not much code! i like.