9lessons programming blog
Loading Search
Sunday, January 24, 2010

PHP Time Stamp Function.

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

PHP Time Stamp Function.

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

Share this post

Comments
{ 28 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)

infopediaonlinehere said...

this is a great script...very few websites present realtime examples like this...you are great...keep up good job dude

Anonymous said...

I would Do it this way


function timesince($original) {
$ta = array(
'year' => 31536000, // year
'month' => 2592000, // month
'week' => 604800, // week
'day' => 86400, // Day
'hour' => 3600, // Hour
'minut' => 60, // Minute
'second' => 1); // Second
$since = time() - $original;
$res = '';
foreach($ta as $key => $value) {
$cnt = floor($since / $value);
if($cnt != 0) {
$since = $since - ($value * $cnt);
if($res == ''){
$res .= ($cnt == 1) ? '1 ' . $key : "$cnt {$key}s";
} else {
$res .= ($cnt == 1) ? ' 1 ' . $key : " $cnt {$key}s";
}
}
}
return $res;
}

Anonymous said...

A little improvment to the last posted version. Is more a facebook style date:

function timesince($original) {
$ta = array(
array(31536000, "Year"),
array(2592000, "Month"),
array(604800, "Week"),
array(86400, "Day"),
array(3600, "Hour"),
array(60, "Minute"),
array(1, "Second")
);

$since = time() - $original;

$res = "";
$lastkey = 0;

for( $i=0; $i= 60 && ($i - $lastkey) == 1 ){
$res .= ($cnt == 1) ? " y 1 {$ta[$i][1]}" : " y {$cnt} {$ta[$i][1]}s";
break;
}else{
break;
}
}
}

return $res;
}

Anonymous said...

hi ..i need a reverse i mean from "Posted 3 hours 26 mins ago" to timestamp

Bruno Oluka said...

my cms posts return posted 40 years ago by: Administrator every time i add a new entry instead of showing may b 1 minue ago.
can some one help me.
Thanks

love and relationship said...

very nice tutorial...great work

datingladys said...

very very interesting post

Big News Articles said...

really great post and usefull

Adrian said...

what should i do if i have the time saved as "2011-07-10T16:32:24+00:00" for example?

Anonymous said...

very usefull and pure php code keep going.

thanks

Anonymous said...

thanks

motore asincrono said...

very interesting post

Anonymous said...

oh??? i have a problem with it... it shows 9hrs ago and it should me a bit of seconds ago will be display.. so what is the problem???

Anonymous said...

how can i apply this code to my project?pls help. huhu

Anonymous said...

Couldn't "one day ago" be "yesterday"?

Post a Comment

Subscribe now!Recent Posts

Categories

Subscribe now!Popular Posts

People Says

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

Smashing Magazine

Like Me

follow me
products

9lessons labs

9lessons clouds

Android application

Chrome Extension