Return to Snippet

Revision: 41778
at February 23, 2011 07:37 by GeorgeL


Initial Code
function countTime($given) {
$current=time();
$passed=$current-$given;
if($given=='')
{
  $passed=100000000000;
}
$months=$passed/2628000;
$weeks=$passed/604800;
$days=$passed/86400;
$hours=$passed/3600;
$minutes=$passed/60;
if($passed>(2628000*12))
{
   $r='Long time';
}
elseif($months>=1)
{
   $rem=$months-round($months,0);
   $months=round($months,0);
   $r=$months." month";
   if($months>1)
   {
      $r=$r.'s';
   }
}
elseif($weeks>=1)
{
   $weeks=round($weeks,0);
   $r=$weeks." week";
   if($weeks>1)
   {
      $r=$r.'s';
   }   
}
elseif($days>=1)
{
   $days=round($days,0);
   $r=$days." day";
   if($days>1)
   {
      $r=$r.'s';
   }
}
elseif($hours>=1)
{
   $hours=round($hours,0);
   $r=$hours." hour";
   if($hours>1)
   {
      $r=$r.'s';
   }
}
elseif($minutes>=1)
{
   $minutes=round($minutes,0);
   $r=$minutes." minute";
   if($minutes>1)
   {
      $r=$r.'s';
   }
}
else
{
   $r=$passed." second";
   if($passed>1)
   {
      $r=$r.'s';
   }
}
return $r;
}

Initial URL
http://www.gruburg.com

Initial Description
Use by sending a unix timestamp to countTime(timestamp). It will return something like 40 seconds , 2 weeks, 1 hour. Then just format to your needs like we do in our app: Someone was here 10 minutes ago.

Initial Title
Time Elapsed Function

Initial Tags
php, unix, web

Initial Language
PHP