Jump to content

time ago function


Onloac

Recommended Posts

Hello. I'm trying to create a function that will turn my timestamp into something like "9 hours ago", "3 days ago", ect.. so far I have the below function which seems to do the job fine. only problem is when i try to include it within a span it always places itself right before it.

 

I use

 echo "<span class=\"smalltext\">".time_stamp($date)."</span>";

 

witht the following function

function time_stamp($session_time)
{
$time_difference = time() - $session_time ;

$seconds = $time_difference ;
$minutes = round($time_difference / 60 );
$hours = round($time_difference / 3600 );
$days = round($time_difference / 86400 );
$weeks = round($time_difference / 604800 );
$months = round($time_difference / 2419200 );
$years = round($time_difference / 29030400 );
// 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 "one hour ago";
  }
  else
  {
   echo "$hours hours ago";
  }

}
//Days
else if($days <= 7)
{

  if($days==1)
  {
   echo "one day ago";
  }
  else
  {
   echo "$days days ago";
   }

}
//Weeks
else if($weeks <= 4)
{

   if($weeks==1)
  {
   echo "one week ago";
   }
  else
  {
   echo "$weeks weeks ago";
  }

}
//Months
else if($months <=12)
{

   if($months==1)
  {
   echo "one month ago";
   }
  else
  {
   echo "$months months ago";
   }

}
//Years
else
{

   if($years==1)
   {
    echo "one year ago";
   }
   else
  {
    echo "$years years ago";
   }

}

} 

in the end I get the span tag with nothing in it and the text right before it. What am I doing wrong?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.