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 minute ago“, “2 days ago“, displayed on Facebook , Twitter etc..
In PHP time();
function returns a Unix Value of current date and time like 123454487. We can convert Unix value to a readable date format using date();
function.
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 '; }
Here is the simple PHP code for display date as Twitter or Facebook style.
If you have any feedback, bug reports please comment below. Happy coding 🙂