0

//$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.

Edited by tyson567: posted code twice accidentally

<?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);
         ?>
3
Contributors
3
Replies
19
Views
5 Years
Discussion Span
Last Post by tyson567
2

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.

0

@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.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.