• support@answerspoint.com

Time Ago Function PHP

656

i want to show my post in display below like facebook style time ago in php function

  • 7 mins ago;

  • 33 seconds ago

  • 17 minutes ago

  • 2 hours ago

  • 3 hours ago

  • 1 day ago

  • 1 week ago

  • 3 weeks ago

  • 7 months ago

  • 10 years ago

  • 72 years ago

Time Ago Function PHP ,  we have want to show it in easy to read way like: 1 minutes ago, 1 hour ago , Today I will show you to use PHP function to display time ago base on timestamp. Here is very simple PHP function what you can use in any PHP , Facebook Style time ago

  • PHP

  • asked 3 years ago
  • Sunny Solu

2Answer


0

The following function will convert the time stamp to time ago like 1hour ago, 1day ago, 2days ago etc.
 

PHP Code
 

<?php
function timeAgo($time_ago){
$cur_time 	= time();
$time_elapsed 	= $cur_time - $time_ago;
$seconds 	= $time_elapsed ;
$minutes 	= round($time_elapsed / 60 );
$hours 		= round($time_elapsed / 3600);
$days 		= round($time_elapsed / 86400 );
$weeks 		= round($time_elapsed / 604800);
$months 	= round($time_elapsed / 2600640 );
$years 		= round($time_elapsed / 31207680 );
// Seconds
if($seconds <= 60){
	echo "$seconds seconds ago";
}
//Minutes
else if($minutes <=60){
	if($minutes==1){
		echo "one minute ago";
	}
	else{
		echo "$minutes minutes ago";
	}
}
//Hours
else if($hours <=24){
	if($hours==1){
		echo "an hour ago";
	}else{
		echo "$hours hours ago";
	}
}
//Days
else if($days <= 7){
	if($days==1){
		echo "yesterday";
	}else{
		echo "$days days ago";
	}
}
//Weeks
else if($weeks <= 4.3){
	if($weeks==1){
		echo "a week ago";
	}else{
		echo "$weeks weeks ago";
	}
}
//Months
else if($months <=12){
	if($months==1){
		echo "a month ago";
	}else{
		echo "$months months ago";
	}
}
//Years
else{
	if($years==1){
		echo "one year ago";
	}else{
		echo "$years years ago";
	}
}
}

?>

 

Call in action

<?php
  $curenttime="2013-07-10 09:09:09";
  $time_ago =strtotime($curenttime);
  echo timeAgo($time_ago);
?>

 

Output

3 years ago

  • answered 2 years ago
  • Sunny Solu

0
function time_elapsed_string($ptime)
{
    $etime = time() - $ptime;

    if ($etime < 1)
    {
        return '0 seconds';
    }

    $a = array( 365 * 24 * 60 * 60  =>  'year',
                 30 * 24 * 60 * 60  =>  'month',
                      24 * 60 * 60  =>  'day',
                           60 * 60  =>  'hour',
                                60  =>  'minute',
                                 1  =>  'second'
                );
    $a_plural = array( 'year'   => 'years',
                       'month'  => 'months',
                       'day'    => 'days',
                       'hour'   => 'hours',
                       'minute' => 'minutes',
                       'second' => 'seconds'
                );

    foreach ($a as $secs => $str)
    {
        $d = $etime / $secs;
        if ($d >= 1)
        {
            $r = round($d);
            return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
        }
    }
}
  • answered 2 years ago
  • Sunny Solu

Your Answer

    Facebook Share        
       
  • asked 3 years ago
  • viewed 656 times
  • active 3 years ago

Hot Questions