Welcome Guest, Not a member yet? Register   Sign In
Posted X ago
#8

[eluser]Michael Wales[/eluser]
The Date Helper has the timespan() function built-in. It can get very specific though - a bit to specific for my tastes, so I use the following.

Just throw it in your own helper file and load it.
Code:
// Probably not an accurate name, since we go down to minutes if need be
function days_ago($seconds = 1) {
  if (!is_numeric($seconds)) {
    $seconds = strtotime($seconds);
  }
  $seconds = now() - $seconds;
  
  if ($seconds < 0) {
    return 'Just now';
  }
  
  $days = floor($seconds / 86400);
  if ($days > 0) {
    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 > 52) {
    return '1 hour ago';
  } elseif ($minutes > 38) {
    return '45 minutes ago';
  } elseif ($minutes > 24) {
    return '30 minutes ago';
  } elseif ($minutes > 10) {
    return '15 minutes ago';
  } else {
    return 'Just now';
  }
}

I take the wierd route of returning the minutes just because I think it looks better. Do the users really care if it was 18 minutes ago? When you read 18 minutes you immediately think, 15 minutes ago. Makes it a bit more user-friendly to round them out to the quarter hour.


Messages In This Thread
Posted X ago - by El Forum - 07-05-2008, 03:16 PM
Posted X ago - by El Forum - 07-05-2008, 04:02 PM
Posted X ago - by El Forum - 07-05-2008, 06:40 PM
Posted X ago - by El Forum - 07-05-2008, 07:15 PM
Posted X ago - by El Forum - 07-05-2008, 07:23 PM
Posted X ago - by El Forum - 07-05-2008, 08:18 PM
Posted X ago - by El Forum - 07-06-2008, 01:05 AM
Posted X ago - by El Forum - 07-06-2008, 08:11 AM
Posted X ago - by El Forum - 07-06-2008, 08:50 PM
Posted X ago - by El Forum - 07-07-2008, 02:28 AM



Theme © iAndrew 2016 - Forum software by © MyBB