phpcodeignitercodeigniter-3helperdate-difference

timespan() is not generating the correct difference between two Y-m-d dates in CodeIgniter


abscondingsince is a date field with value 2016-08-24 and dateofcontactviaphone is a date field with value 2016-08-26.

When I try to use CodeIgniter's date helper function timespan() like this:

echo timespan($row['abscondingsince'], $row['dateofcontactviaphone']);

it's giving 2016-08-2446 Years, 8 Months, 3 Days, 5 Hours, 43 Minutes as the output. This is obviously wrong because the dates are only 2 days apart.


Solution

  • Supply your dates to the function as Unix timestamps like this:

    echo timespan(
        strtotime($row['abscondingsince']), 
        strtotime($row['dateofcontactviaphone'])
    );