Sunday, January 24, 2010

PHP Time Stamp Function.

13 comments
I had developed a date time stamp function using PHP and implemented this at labs.9lessons.info. It is very important for social media web applications. I hope it's useful for you.

Facebook Wall Script 2.0 Download link : http://labs.9lessons.info/status.php?msgid=150



Download Script     Live Demo

time_stamp.php
Contains PHP code. You have to use the time_stamp() function to display the date and time.
<?php
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";
   }

}

}
$session_time ="1264326122";
//$session_time=time();
echo time_stamp($session_time);
?>
Sponsored Links

Recent Posts

Share this post

Subscribe to my feeds

Subscribe
Comments
13 comments
Julius Beckmann said...

Hi,
i would write this function this way:
http://nopaste.info/465ae57b7f_nl.html

1. Use return not echo in a function.
2. Your formatting was BAD.

karthikeyan said...

excellent script... really nice

Derleth said...

vaya, el script es muy util, tiene buena pinta y es justo lo que necesitaba.. muchas gracias chato :)

Master D said...

I don't think timestamp are the way to go. Rather use normale date represetations.

Anonymous said...

first time such a light post.. you should write something more important.

Alec Keyl said...

Here what I wrote long before myspace and facebook.

http://nopaste.info/5537800c28.html

Anonymous said...

nice Indian code =)

Try ZF/Symfony

Anonymous said...

nice script.. excellent job..!!

UrbanTwitch said...

Very nice job. You're like a genius at this.

fare said...

the perfect code thanx Srinivas Tamada ;)

Anonymous said...

hi guys!here is how i`m saving time in the db,the date field is timestamp.
but when i try to load it is gives me 44 years ago.

$session_time="2010-03-03 00:01:51";
$time=time_stamp($session_time);
So what i`m i doing wrong here

Anonymous said...

well i got it working
$session_time="2010-03-03 00:01:51";
$time=time_stamp($session_time);

Klian said...

Try with:

$session_time= strtotime("2010-03-03 00:01:51");
$time=time_stamp($session_time)

Post a Comment

Orkut | FacebookAbout Me

Subscribe now!Feeds RSS

Labs ProfileRelease

Subscribe now!Recent Posts

Subscribe now!Categories

Subscribe now!Comments

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Join into my community

My ProfileTwitter