开发者

How can I specify how many levels deep the timespan() function in Codeigniter should go?

开发者 https://www.devze.com 2022-12-26 21:35 出处:网络
I\'m using Codeigniter\'开发者_Python百科s \"timespan()\" function to give a human readable phrase of how much time has elapsed since an event: \"5 days, 3 hours, 12 minutes, 1 second\", but I don\'t

I'm using Codeigniter'开发者_Python百科s "timespan()" function to give a human readable phrase of how much time has elapsed since an event: "5 days, 3 hours, 12 minutes, 1 second", but I don't need to go down to the minute or second level.

Is there any way to turn that off?


Short answer: no.

The timespan() method doesn't provide any means to customize the level of detail in its output. CodeIgniter does, though, make it pretty easy to override built-in functions so they do what you like.

There may be a more elegant solution (see below), but the following is what I came up with. Create a file called MY_date_helper.php in application/helpers and put the following modification to timespan() into it:

<?php
// Adds a third argument to timespan() that stops display of minutes/
// seconds in the final output
function timespan($seconds = 1, $time = '', $display_mins_secs = true)
{
    $CI =& get_instance();
    $CI->lang->load('date');

    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')).', ';
    }   

    $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')).', ';
        }   

        $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')).', ';
        }

        $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')).', ';
        }

        $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')).', ';
        }

        $seconds -= $hours * 3600;
    }

    // don't display minutes/seconds unless $display_mins_secs
    // == true
    if ($display_mins_secs)
    {
        $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')).', ';
            }

            $seconds -= $minutes * 60;
        }

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

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

?>

CodeIgniter will automagically find this file and use your timespan() function instead of the built-in version. The "Extending Helpers" section of this page offers a little more information about how this works.

To take advantage of the "extended" timespan() function, just pass a third argument of false, like this:

$variable = timespan($secs, $time, false);

There's possibly a more elegant solution, maybe using PHP's native DateTime object (see here), but from the question I think the above timespan() modification more closely fits your needs.


I know this is an old thread but I just found it and was almost the solution for what I was looking for. I just wanted to have a level limit by number. I did a small modification in the script where I make the $str as an array and add each year, month, day etc. Then I walk through the array until the given limit. After that I just implode a ", " and return the result.

Maybe someone else also can enjoy this script?

<?php
// Adds a third argument to timespan() that limits the text string at given level
function timespan($seconds = 1, $time = '', $limit=null)
{
    $CI =& get_instance();
    $CI->lang->load('date');

    if ( ! is_numeric($seconds))
    {
        $seconds = 1;
    }

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

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

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

    if ($years > 0)
    {   
        $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year'));
    }   

    $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'));
        }   

        $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'));
        }

        $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'));
        }

        $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'));
        }

        $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'));
        }

        $seconds -= $minutes * 60;
    }

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

    // Only show number of array elements depending on limit.
    if($limit)
    {
        $i = 0;
        foreach($str as $level)
        {
            if($i >= $limit)
                unset($str[$i]);
            $i++;
        }
    }

    $return = implode(', ', $str);

    return trim($return);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消