Tuesday, May 6, 2014

Timeago using PHP

Time ago function is very important for social websites.In this post i will shows how to add time ago function in website using php.



I have used time() functions to display time ago.

time()
It is used  to return current time


PHP Code

   <?php
function time_stamp($session_time)
{
$time_diff = time() - $session_time ;
$seconds = $time_diff ;
$minutes = round($time_diff / 60 );
$hours = round($time_diff / 3600 );
$days = round($time_diff / 86400 );
$weeks = round($time_diff / 604800 );
$months = round($time_diff / 2419200 );
$years = round($time_diff / 29030400 );

//For Seconds
if($seconds <= 60)
{
    echo "Updated $seconds seconds ago";
}
//For Minutes
else if($minutes <=60)
{
    if($minutes==1)
    {
        echo "Updated one minute ago";
    }
    else
    {
        echo "Updated $minutes minutes ago";
    }
}
//For Hours
else if($hours <=24)
{
    if($hours==1)
    {
        echo "Updated one hour ago";
    }
    else
    {
        echo "Updated $hours hours ago";
    }
}
//For Days
else if($days <= 7)
{
    if($days==1)
    {
        echo " Updated one day ago";
    }
    else
    {
        echo "Updated $days days ago";
    }
}
//For  Weeks
else if($weeks <= 4)
{
    if($weeks==1)
    {
        echo "Updated one week ago";
    }
    else
    {
        echo "Updated $weeks weeks ago";
    }
}
//For  Months
else if($months <=12)
{
    if($months==1)
    {
        echo "Updated one month ago";
    }
    else
    {
        echo "Updated $months months ago";
    }
}
//For Years
else
{
    if($years==1)
    {
        echo "Updated one year ago";
    }
    else
    {
        echo "Updated $years years ago";
    }
}
}
//Session time
$session_time ="1399358058";
echo time_stamp($session_time);
?>
Please share your comments and feedback.Thanks.

1 comment: