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

[eluser]rich.a.coy[/eluser]
Is there a way to shorten the timespan() function to show just the first segment?

For example: "6 hours", or "23 minutes"? Whichever is the biggest time value available.

No one wants to see that my database record is "1 Week, 4 Days, 21 Hours, 52 Minutes" old. Smile

I saw an old thread on this issue but did not see a solution.

Thanks.
#2

[eluser]rich.a.coy[/eluser]
I found close to want I needed in this thread:

http://ellislab.com/forums/viewthread/113067/

However is was not precise enough so I modified it to display down to minutes and seconds. If you need this functionality just put this code in a helper file for the date_helper and call the elapsed_time function.

Code:
<?php
// Elapsed Time
function elapsed_time($seconds = 1) {
  if (!is_numeric($seconds)) {
    $seconds = strtotime($seconds);
  }
  $seconds = now() - $seconds;
  
  $days = floor($seconds / 86400);
  if ($days > 0) {
      if ($days == 1) {
          return '1 day ago';
      }
    return $days . ' days ago';
  }
  
  $hours = floor($seconds / 3600);
  if ($hours > 0) {
    if ($hours == 1) {
      return '1 hour ago';
    }
    return $hours . ' hours ago';
  }
  
  $minutes = floor($seconds / 60);
  if ($minutes > 0) {
    if ($minutes == 1) {
      return '1 minute ago';
    }
    return $minutes . ' minutes ago';
  }
  
  if ($seconds <= 1) {
    return 'Just now';
   } else {
    return $seconds . ' seconds ago';
  }
}

Hope this helps someone.
#3

[eluser]ebot[/eluser]
you look at this i made my self and added the codes on the date help you can use it directly like how you use the codeigniter help ok. if anything feel free to contact me at ebot.tabi[at]gmail[dot]com:

here is the link this forum: http://ellislab.com/forums/viewthread/131489/
Code:
if ( ! function_exists('when'))
{
    function when($dt,$precision=2)

    {
    $times=array(    365*24*60*60    => "year",
                    30*24*60*60        => "month",
                    7*24*60*60        => "week",
                    24*60*60        => "day",
                    60*60            => "hour",
                    60                => "minute",
                    1                => "second");

    $passed=time()-$dt;

    if($passed<5)
    {
        $output='less than 5 seconds ago';
    }elseif($passed > 172800){
         $output=date("jS F,Y",$dt);
    }
    else
    {
        $output=array();
        $exit=0;

        foreach($times as $period=>$name)
        {
            if($exit>=$precision || ($exit>0 && $period<60)) break;

            $result = floor($passed/$period);
            if($result>0)
            {
                $output[]=$result.' '.$name.($result==1?'':'s');
                $passed-=$result*$period;
                $exit++;
            }
            else if($exit>0) $exit++;
        }

        $output=implode(', ',$output).' ago';
    }

    return $output;
}




Theme © iAndrew 2016 - Forum software by © MyBB