codeigniter timespan function

被刻印的时光 ゝ 提交于 2020-01-25 02:54:27

问题


Hello i have now search the hole web and found a lot but i just dont know how to make it to work so now im asking here for help

i want to do so then a person create a comment it should said "created 1 sec. ago" and then 1 min and 1 hour and like that :)

can some one help me with that ?

thanks


回答1:


This is basically human readable format, and can be completed by mathematical checks to check the distance of times, working snippet below:

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;
}

This will do future timestamps such as 12 days to go aswell as timestamps such as 12 days ago

Hope this helps.

Original Source: http://blog.evandavey.com/2008/04/php-date-in-human-readable-form-facebook-style.html




回答2:


I think this is exactly what you want. When you using the function set $deep parameter to 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




回答3:


Assuming you have the difference $now - $creation_time in seconds, a way to do it is to divide it by X seconds (1 minute = 60, 1 hour = 3600, 1 day = 86400) starting with the largest number to see how many of those units fit in your creation time, then use its remainder to try and fit the smaller units in.

$diffSeconds = time() - $creation_time ;

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

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

The modulo operator % will give you the remainder of a division. This way, if a post was created less than a day ago then $numDays is 0 and $remainderDaySeconds is $diffSeconds, so you can check and print out accordingly.

Edit I got curious and looked in SO, turns out there are quite a few questions expanding on this. Linking some:

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



来源:https://stackoverflow.com/questions/4504516/codeigniter-timespan-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!