如何在编码中从时间跨度中获得天数


How to get number of days back from timespan in codeingiter

下面代码的输出是2 Weeks, 1 Day。而我想打印的天数,如15天等。如何做到这一点,我对此毫无头绪。

<?php 
echo timespan(strtotime($row['abscondingsince']), 
               strtotime($row['dateofcontactviaphone']));
?>

我在我的一个codeignator函数中使用了下面的函数,它工作得很好,希望对你有所帮助。

echo show_date_string('12-07-2016');
function show_date_string($datetime) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);
    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;
    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}