如何在代码中获取从时间跨度返回的天数

时间:2016-08-24 12:23:02

标签: php codeigniter web-applications codeigniter-2 timespan

以下代码的输出为2周,1天。而我想在15天等的天数打印,依此类推。如何实现这一点,我对此毫无头绪。

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

1 个答案:

答案 0 :(得分:0)

我在我的一个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';
}
相关问题