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 Demotime_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);
?>
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);
?>














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.
excellent script... really nice
vaya, el script es muy util, tiene buena pinta y es justo lo que necesitaba.. muchas gracias chato :)
I don't think timestamp are the way to go. Rather use normale date represetations.
first time such a light post.. you should write something more important.
Here what I wrote long before myspace and facebook.
http://nopaste.info/5537800c28.html
nice Indian code =)
Try ZF/Symfony
nice script.. excellent job..!!
Very nice job. You're like a genius at this.
the perfect code thanx Srinivas Tamada ;)
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
well i got it working
$session_time="2010-03-03 00:01:51";
$time=time_stamp($session_time);
Try with:
$session_time= strtotime("2010-03-03 00:01:51");
$time=time_stamp($session_time)
this is a great script...very few websites present realtime examples like this...you are great...keep up good job dude
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;
}
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;
}
hi ..i need a reverse i mean from "Posted 3 hours 26 mins ago" to timestamp
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
very nice tutorial...great work
very very interesting post
really great post and usefull
what should i do if i have the time saved as "2011-07-10T16:32:24+00:00" for example?
very usefull and pure php code keep going.
thanks
thanks
very interesting post
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???
how can i apply this code to my project?pls help. huhu
Couldn't "one day ago" be "yesterday"?