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

[eluser]stuffradio[/eluser]
Is there a helper that can give me this result? Say if I posted a comment, I want to be able to see Comment 12345 "posted 20 seconds ago."

Thanks!
#2

[eluser]richthegeek[/eluser]
you store a timestamp with the comment by using time().

To get the number of seconds ago, take the timestamp away from the current time().

You can use various divisions (60, 3600, 86400) to get the number of minutes, hours, and/or days ago.
#3

[eluser]stuffradio[/eluser]
Can you tell me what it is?

This
Code:
$minutes = $images->time - time() / 3600;
Gives me this.
Code:
Posted:  Minutes 1214965913.3422 ago.
#4

[eluser]EEssam[/eluser]
I'm looking for this as well...
#5

[eluser]richthegeek[/eluser]
You need to wrap your sums...
Code:
$minutes = round( ( $images->time - time() ) / 3600 );

This would be best used in the form of a model, so that your images model would retrieve the data from the database and work out the number of minutes, hours, seconds, days, etc ago - either automagically or via a direct function call.
#6

[eluser]stuffradio[/eluser]
It tells me -2 :S
#7

[eluser]richthegeek[/eluser]
hey man, my bad XD

take the Image->time from teh current time, not the current way.

Code:
$minutes = round( ( time() - $images->time ) / 3600 );
#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.
#9

[eluser]stuffradio[/eluser]
Thanks,

I'll try it out and let you know!
#10

[eluser]avinashv[/eluser]
[quote author="Michael Wales" date="1215371484"]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.[/quote]

In that case, I would probably make it explicit that you're being fuzzy with the time ala "about 15 minutes ago" rather than straight up write 15 minutes.

Handy function though, I'm going to hang on to it, thanks.




Theme © iAndrew 2016 - Forum software by © MyBB