Welcome Guest, Not a member yet? Register   Sign In
how to trim timespan()
#1

[eluser]newbie boy[/eluser]
hi guys....

i need to trim down the result of timespan()...

so if the interval totals to a day(s)... it will only show the day....

same for the hour(s) or so....


thanks guys....
#2

[eluser]NogDog[/eluser]
First thing that comes to mind:
Code:
$output = str_replace('(s)', '', $timespan_string);
#3

[eluser]newbie boy[/eluser]
badly needed help guys...

thanks...
#4

[eluser]xwero[/eluser]
When i checked the function just did that.
#5

[eluser]newbie boy[/eluser]
xwero just how?
#6

[eluser]xwero[/eluser]
As i understand it you want to round the timespan function to the last meaningful segment
Code:
echo timespan(time(),strtotime('+2 DAY'))
Echos 2 days. So i don't know what the problem is?
#7

[eluser]newbie boy[/eluser]
here's my ouput:

6 Days, 8 Hours, 7 Minutes ago from the site.


i need to trim down the result of timespan()...

so if the interval totals to a day(s)... it will only show the day….

same for the hour(s) or so….

thanks again....
#8

[eluser]Thorpe Obazee[/eluser]
Code:
if ( ! function_exists('timespan'))
{
    function timespan($seconds = 1, $time = '')
    {
        $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')).', ';
            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')).', ';
            }    
            $seconds -= $months * 2628000;
            return substr(trim($str), 0, -1);
        }

        $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;
            return substr(trim($str), 0, -1);
        }            

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

Something I quickly edited. There might be some errors but you get the idea.

This is assuming I get what you mean by 'trimming'.
#9

[eluser]überfuzz[/eluser]
If you don't want to calculate the days, hours and minutes you could go with a cruder approach.
Seems you're putting out a string where somethings are separated by ,.
Code:
$array = explode(',',$your_string); //use explode to separate the string into an array.
print_r($array); //should give something like
Array
(
   [0] = Days 6
   [1] = Hours 27
   [2] = Minutes 53
)
#10

[eluser]Dam1an[/eluser]
@uberfuzz: Its amazing how we often overlook such a simple solution, good job Smile




Theme © iAndrew 2016 - Forum software by © MyBB