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 : https://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);
?>
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,
ReplyDeletei 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
ReplyDeletevaya, el script es muy util, tiene buena pinta y es justo lo que necesitaba.. muchas gracias chato :)
ReplyDeleteI don't think timestamp are the way to go. Rather use normale date represetations.
ReplyDeletefirst time such a light post.. you should write something more important.
ReplyDeleteHere what I wrote long before myspace and facebook.
ReplyDeletehttp://nopaste.info/5537800c28.html
nice Indian code =)
ReplyDeleteTry ZF/Symfony
nice script.. excellent job..!!
ReplyDeleteVery nice job. You're like a genius at this.
ReplyDeletethe perfect code thanx Srinivas Tamada ;)
ReplyDeletehi guys!here is how i`m saving time in the db,the date field is timestamp.
ReplyDeletebut 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
ReplyDelete$session_time="2010-03-03 00:01:51";
$time=time_stamp($session_time);
Try with:
ReplyDelete$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
ReplyDeleteI would Do it this way
ReplyDeletefunction 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:
ReplyDeletefunction 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
ReplyDeletemy cms posts return posted 40 years ago by: Administrator every time i add a new entry instead of showing may b 1 minue ago.
ReplyDeletecan some one help me.
Thanks
very nice tutorial...great work
ReplyDeletevery very interesting post
ReplyDeletereally great post and usefull
ReplyDeletewhat should i do if i have the time saved as "2011-07-10T16:32:24+00:00" for example?
ReplyDeletevery usefull and pure php code keep going.
ReplyDeletethanks
very interesting post
ReplyDeleteoh??? 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???
ReplyDeletehow can i apply this code to my project?pls help. huhu
ReplyDeleteCouldn't "one day ago" be "yesterday"?
ReplyDeleteThanks you very nice,
ReplyDeleteGreat job on the script.
ReplyDeletehow do you make it loop. like if you have so many reply's in a post. do you put it in side a "while" loop? thanks
ReplyDeleteI like your article!
ReplyDeletecool article and script... keep up the good work...
ReplyDeleteVery nice job. Thanks!
ReplyDeleteThank you for the script
ReplyDeleteUseful program. Thanks
ReplyDeleteThanks for the post
ReplyDeleteVery Helpful Article..
ReplyDeleteGood work, was very useful to me :)
ReplyDeleteExcellent Stuff, Working like a charm ! thanks Srinivas
ReplyDeleteHow to show the time without echo?
ReplyDeleteHow to show this time. Without putting echo in the function? instead of putting echo in my own php? Thanks
ReplyDelete