Twitist Forums
How to create facebook or twitter like x-time ago date/time format in PHP? - Printable Version

+- Twitist Forums (http://twitist.com)
+-- Forum: Twitter forums (/forum-1.html)
+--- Forum: Twitter General help (/forum-6.html)
+--- Thread: How to create facebook or twitter like x-time ago date/time format in PHP? (/thread-1872.html)



How to create facebook or twitter like x-time ago date/time format in PHP? - Salsan Jose - 10-01-2012 09:57 AM

How to create facebook or twitter like x-time ago date/time format in PHP? Is there any built in function are available? Any help would be appreciated.


- salsanej - 10-01-2012 10:05 AM

Its possible with a custom made PHP function. Please find the source to get more idea about this.


function ShowDate($date) // $date --> time(); value
{
$stf = 0;
$cur_time = time();
$diff = $cur_time - $date;
$phrase = array('second','minute','hour','day','week','month','year','decade');
$length = array(1,60,3600,86400,604800,2630880,31570560,315705600);
for($i =sizeof($length)-1; ($i >=0)&&(($no = $diff/$length[$i])<=1); $i--); if($i < 0) $i=0; $_time = $cur_time -($diff%$length[$i]);
$no = floor($no); if($no <> 1) $phrase[$i] .='s'; $value=sprintf("%d %s ",$no,$phrase[$i]);
if(($stf == 1)&&($i >= 1)&&(($cur_tm-$_time) > 0)) $value .= time_ago($_time);
return $value.' ago ,';
}


This function can display time or date as 1 days ago, 2 days ago, etc..