Jump to content

[SOLVED] Time difference function


psychohagis

Recommended Posts

Does anyone know a good function that will say how long ago a post was for example.
I found this one:
[code]function kiedy($ts) {
  $ts=time()-$ts;
  if ($ts<60)                   
      // <1 minute
      return $ts." seconds ago";
    elseif ($ts<60*60)                     
      // <1 hour
      return floor($ts/60)." minutes ago";
  elseif ($ts<60*60*2)             
      // <2 hour
      return "1 hour ago";
    elseif ($ts<60*60*24)             
      // <24 hours = 1 day
      return floor($ts/60*60)." hours ago";
    elseif ($ts<60*60*24*2)           
      // <2 days
      return "1 day ago";
    elseif ($ts<60*60*24*7)           
      // <7 days = 1 week
        return floor($ts/60*60*24)." days ago";
  elseif ($ts<60*60*24*30.5)           
      // <30.5 days ~  1 month
      return floor($ts/60*60*24*7)." weeks ago";
    elseif ($ts<60*60*24*365)           
      // <365 days = 1 year
      return floor($ts/60*60*24*30.5)." months ago";
  else         
      // more than 1 year
      return floor($ts/60*60*24*365)." years ago";
};
[/code]

But it does not work. When I run a Timestamp for a post I made 3 hours ago it says [b]12527 hours ago [/b]
Link to comment
https://forums.phpfreaks.com/topic/33119-solved-time-difference-function/
Share on other sites

Solved, I found this one which seems to work:

[code]function ago($timestamp){
      $difference = time() - $timestamp;

      if($difference < 60)
          return $difference." seconds ago";
      else{
          $difference = round($difference / 60);
          if($difference < 60)
              return $difference." minutes ago";
          else{
              $difference = round($difference / 60);
              if($difference < 24)
                  return $difference." hours ago";
              else{
                  $difference = round($difference / 24);
                  if($difference < 7)
                      return $difference." days ago";
                  else{
                      $difference = round($difference / 7);
                      return $difference." weeks ago";
                  }
              }
          }
      }
  }[/code]
<?php
function timeago($timestamp){
$current_time = time();
$difference = $current_time - $timestamp;
$periods = array("minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array(60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
for($val = sizeof($lengths) - 1; ($val >= 0) && (($number = $difference / $lengths[$val]) < 1); $val--);
if($val < 0) $val = 0;
$new_time = $current_time - ($difference % $lengths[$val]);
$number = floor($number);
if($number != 1) $periods[$val].= "s";
$text = sprintf("%d %s ", $number, $periods[$val]);   
if(($val >= 1) && (($current_time - $new_time) > 0)) $text .= timeago($new_time);
return $text;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.