1 <? PHP 2 // timestamp converted to minutes, hours, days, weeks, months, years ago 3 function timeFormat ( $ Time ) 4 { 5 $ Time = (int) substr ( $ Time , 0, 10 ); . 6 $ int = Time () - $ Time ; . 7 $ STR = '' ; . 8 IF ( $ int <= 30 ) { . 9 $ STR = sprintf ( 'just', $ int ); 10 } ELSEIF ($ int <60 ) { . 11 $ STR = sprintf ( '% D seconds ago', $ int ); 12 is } ELSEIF ( $ int <3600 ) { 13 is $ STR = sprintf ( '% D minutes ago', Floor ( $ int / 60 )); 14 } ELSEIF ( $ int <86400 ) { 15 $ STR = sprintf ( '% D hours before', Floor ( $ int / 3600 )); 16 } ELSEIF ( $ int<604800 ) { . 17 $ STR = sprintf ( '% D days before', Floor ( $ int / 86400 )); 18 is } ELSEIF ( $ int <2592000 ) { . 19 $ STR = sprintf ( '% D Weeks Ago', Floor ( $ int / 604800 )); 20 is } ELSEIF ( $ int <31536000 ) { 21 is $ STR = sprintf ( '% D months ago', Floor ( $ int / 2592000 )); 22 is } elseif ($int < 946080000) { 23 $str = sprintf('%d年前', floor($int / 31536000)); 24 } else { 25 $str = date('Y-m-d H:i:s', $time); 26 } 27 return $str; 28 } 29 30 echo timeFormat($time = 1564588800);