Time Ago PHP function / script

Ok guys, do you have social network site, or forum, or whatever site did you want to show the event time in Time Ago PHP format?

This script will help you to do that – in php.
This script can be used for comments or whatever you want to tell the time ago instead of the exact time. This php method will be correct to some one in another time zone.

Time Ago PHP

So let’s see the php function script:

<?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";
    }
}
}
 
?>
<?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";
	}
}
}

?>

This was the function declaration.
If you want to use the PHP function / script in the code, just call it like that:

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

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz