Normally date formats are like “ January 1 2010 10:40:25 GMT” , “01 Mar 2010” . etc . But here I tell you ‘how to show date like “1 minutes ago” , “2 days ago” , displayed in Facebook , Twitter etc..
In php time(); function returns a Unix Value of current date and time like 123454487 . We can convert Unix value to readable date format using date(); function.
Here is the simple php code for display date as Twitter or Facebook style .
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 '; }
If you have any feedback, bug reports please comment below. Happy coding





Thanks dude……
this is the code I’m looking for.
Pretty nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!
help me! How do I use in my blogspot? I am a beginner
Hi…
I have downloaded this script, and it’s given me an error on line 18. Would you please help me find the error?
Hi Dana,
Please change the “&” to “&” in the script and try again
dear i tried your script but browser throw and error like this.
Parse error: parse error, expecting `’)” in C:wampwwwcrawltesting.php on line 11.
i also change “&” to “&”. but the still a parse error kindly help me im beginner.
Thanx in advance…
i think the problem is here…
for($i =sizeof($length)-1; ($i >=0)&&(($no = $diff/$length[$i])< =1); $i–); if($i < 0) $i=0; $_time = $cur_time -($diff%$length[$i]);
&&
im wating please help me…
correction of code :
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 ‘;
}