Welcome Guest, Not a member yet? Register   Sign In
timespan() shortening
#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.


Messages In This Thread
timespan() shortening - by El Forum - 01-15-2010, 07:05 PM
timespan() shortening - by El Forum - 01-16-2010, 06:00 AM
timespan() shortening - by El Forum - 01-16-2010, 01:04 PM



Theme © iAndrew 2016 - Forum software by © MyBB