Time Ago function in PHP

tyson567 0 Tallied Votes 1K Views Share

//$time in the second php code hold the time stored in the database in the same format as above(ie TIMESTAMP Format)

//Save the first php code as timeago.php
//include first php code u saved in the second php code as i have done..
//Use the Second Php code Wherever You Want to show time ago.

<?php
//Php Time_Ago Script v1.0.0
//Scripted by D.Harish Kumar@TYSON567
 function time_stamp($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";
    }
 
}
 
}
?>

 <?
        include 'timeago.php';
        $time="2012-07-05 15:47:01";
        $time_ago =strtotime($time);
         echo time_stamp($time_ago);
         ?>
pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

This is also available in the PHP manual...

Member Avatar for diafol
diafol

I'm not sure about the month calculation. This will be different for different current months. Also have to ensure that daylight saving can be ignored or taken into account, so for the function parameters, you may need to include daylight saving and timezone as optionals. Dividing/multiplying by seconds is always a bit dicey when it comes to daylight saving and sometimes leap years. The datetime->diff() may help with these calculations. As the implementation seems to be a relatively broad description of time ago, not "no. months, no. weeks, .... no. seconds", it probably doesn't matter too much.

tyson567 1 Newbie Poster

@diafol You are correct but i wanted to create this script that almost look like Daniweb's script in it's post which will help newbies to use in their site...Since i have my exams in august i may create a new script with daylight saving and exact math for months later that take the corresponding month also into account.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.