First, the function realization
Example 1:
function Time_tran ($the _time) {$now _time= Date ("y-m-d h:i:s", Time () +8* -* -); $now _time=strtotime ($now _time); $show _time=strtotime ($the _time); $dur= $now _time-$show _time; if($dur <0){ return$the _time; }Else{ if($dur < -){ return$dur.'seconds ago'; }Else{ if($dur <3600){ returnFloor ($dur/ -).'minutes ago'; }Else{ if($dur <86400){ returnFloor ($dur/3600).'hours ago'; }Else{ if($dur <259200){//within 3 days returnFloor ($dur/86400).'days ago'; }Else{ return$the _time; } } }
Example 2:
<?phpfunction format_date ($time) {$t=time ()-$time; $f=Array ('31536000'='years', '2592000'='a month', '604800'='Week', '86400'='days', '3600'='hours', ' -'='minutes', '1'='seconds' ); foreach($f as$k =$v) { if(0! = $c =floor ($t/(int) ( $k)) {return$c. $v.'before'; } }}
Example 3:
function Formattime ($date) {$str="'; $timer=Strtotime ($date); $diff= $_server['Request_time'] -$timer; $day= Floor ($diff/86400); $free= $diff%86400;if($day >0) {return$day."days ago";}Else{if($free >0) {$hour= Floor ($free/3600); $free= $free%3600;if($hour >0){return$hour."hours ago";}Else{if($free >0) {$min= Floor ($free/ -); $free= $free% -;if($min >0){return$min."minutes ago";}Else{if($free >0){return$free."seconds ago";}Else{return 'just';}}}Else{return 'just';}}}Else{return 'just';}}}
Example 4:
function Time_tran ($the _time) {$now _time= Date ("y-m-d h:i:s", Time () +8* -* -); $now _time=strtotime ($now _time); $show _time=strtotime ($the _time); $dur= $now _time-$show _time;if($dur <0){return$the _time;}Else{if($dur < -){ return$dur.'seconds ago'; }Else{ if($dur <3600){ returnFloor ($dur/ -).'minutes ago'; }Else{ if($dur <86400){ returnFloor ($dur/3600).'hours ago'; }Else{ if($dur <259200){//within 3 days returnFloor ($dur/86400).'days ago'; }Else{ return$the _time; } } }}}}
PHP calculates a few ago, hours ago, days ago several functions