Calculate days from a point in time?

This file works for dates in the 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);

}

      

Can anyone help brother?

+2


source to share


1 answer


Instead of calculating timestamps from two dates given as parameters, you can simply get those timestamps as parameters directly; those. 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, string time gets the timestamp corresponding to the date; -)


For example, this:



var_dump(dateDiffTs(1251151200, 1251410400));

      

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

And it works if you also have clocks on timestamps:

var_dump(dateDiffTs(1251196200, 1251443700));

      

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

+9


source







All Articles