codeigniter时间跨度函数

时间:2010-12-21 22:23:43

标签: php codeigniter echo timespan

您好我现在已经搜索了漏洞网并发现了很多但我不知道如何让它工作所以现在我在这里寻求帮助

我想这样做,然后一个人创建一个评论它应该说“创建1秒前”,然后1分钟和1小时,就像那样:)

有人可以帮助我吗?

感谢

3 个答案:

答案 0 :(得分:1)

这基本上是人类可读的格式,可以通过数学检查来完成,以检查时间的距离,下面的工作片段:

function RelativeTime($timestamp)
{
    $difference = time() - $timestamp;
    $periods = array("sec", "min", "hour", "day", "week", "month", "years", "decade");
    $lengths = array("60","60","24","7","4.35","12","10");

    if ($difference > 0)
    {
        $ending = "ago";
    }
    else
    {
         $difference = -$difference;
         $ending = "to go";
    }

    for($j = 0; $difference >= $lengths[$j]; $j++)
    {
        $difference /= $lengths[$j];
    }

    $difference = round($difference);

    if($difference != 1)
    {
         $periods[$j].= "s";
    }
    return $difference . $periods[$j] . $ending;
}

这将执行12 days to go等未来时间戳以及12 days ago等时间戳

希望这有帮助。

原始来源:http://blog.evandavey.com/2008/04/php-date-in-human-readable-form-facebook-style.html

答案 1 :(得分:1)

我认为这正是你想要的。当您使用函数set $ deep参数为1时。

function timespan($seconds = 1, $time = '', $deep = NULL)
{
    $CI = & get_instance();
    $CI->lang->load('date');
    $current_deep = 0;
    if (!is_numeric($seconds))
    {
        $seconds = 1;
    }

    if (!is_numeric($time))
    {
        $time = time();
    }

    if ($time <= $seconds)
    {
        $seconds = 1;
    }
    else
    {
        $seconds = $time - $seconds;
    }

    $str = '';
    $years = floor($seconds / 31536000);

    if ($years > 0)
    {
        $str .= $years . ' ' . $CI->lang->line((($years > 1) ? 'date_years' : 'date_year')) . ', ';
        if (++$current_deep == $deep)
            return substr(trim($str), 0, -1);
    }

    $seconds -= $years * 31536000;
    $months = floor($seconds / 2628000);

    if ($years > 0 OR $months > 0)
    {
        if ($months > 0)
        {
            $str .= $months . ' ' . $CI->lang->line((($months > 1) ? 'date_months' : 'date_month')) . ', ';
            if (++$current_deep == $deep)
                return substr(trim($str), 0, -1);
        }

        $seconds -= $months * 2628000;
    }

    $weeks = floor($seconds / 604800);

    if ($years > 0 OR $months > 0 OR $weeks > 0)
    {
        if ($weeks > 0)
        {
            $str .= $weeks . ' ' . $CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')) . ', ';
            if (++$current_deep == $deep)
                return substr(trim($str), 0, -1);
        }

        $seconds -= $weeks * 604800;
    }

    $days = floor($seconds / 86400);

    if ($months > 0 OR $weeks > 0 OR $days > 0)
    {
        if ($days > 0)
        {
            $str .= $days . ' ' . $CI->lang->line((($days > 1) ? 'date_days' : 'date_day')) . ', ';
            if (++$current_deep == $deep)
                return substr(trim($str), 0, -1);
        }

        $seconds -= $days * 86400;
    }

    $hours = floor($seconds / 3600);

    if ($days > 0 OR $hours > 0)
    {
        if ($hours > 0)
        {
            $str .= $hours . ' ' . $CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')) . ', ';
            if (++$current_deep == $deep)
                return substr(trim($str), 0, -1);
        }

        $seconds -= $hours * 3600;
    }

    $minutes = floor($seconds / 60);

    if ($days > 0 OR $hours > 0 OR $minutes > 0)
    {
        if ($minutes > 0)
        {
            $str .= $minutes . ' ' . $CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')) . ', ';
            if (++$current_deep == $deep)
                return substr(trim($str), 0, -1);
        }

        $seconds -= $minutes * 60;
    }

    if ($str == '')
    {
        $str .= $seconds . ' ' . $CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')) . ', ';
    }

    return substr(trim($str), 0, -1);
}

Source

答案 2 :(得分:0)

假设您在几秒钟内有差异$now - $creation_time,一种方法是将它除以X秒(1分钟= 60,1小时= 3600,1天= 86400),从最大数字开始看看这些单位中有多少适合你的创作时间,然后使用其余部分来尝试适应较小的单位。

$diffSeconds = time() - $creation_time ;

$numDays = $diffSeconds / 86400 ;
$remainderDaySeconds = $diffSeconds % 86400 ;

$numHours = $remainderDaySeconds / 3600 ; 
$remainderSeconds = $remainderDaySeconds % 3600 ;

模运算符%将为您提供除法的余数。这样,如果帖子的创建时间不到一天,那么$numDays0$remainderDaySeconds$diffSeconds,因此您可以相应地进行检查和打印。

编辑我很好奇并且看着SO,结果发现有很多问题正在扩展。链接一些:

Calculate relative time in C# calculating and showing a date as 'secs ago', 'mins ago', 'hours ago' etc指向http://www.php.net/manual/en/function.time.php#89415