PHP 两个时间差 自动返回多少天之前
1 function nicetime($date) 2 { 3 if(empty($date)) { 4 return "No date provided"; 5 } 6 7 $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); 8 $lengths = array("60","60","24","7","4.35","12","10"); 9 10 $now = time(); 11 $unix_date = strtotime($date); 12 13 // check validity of date 14 if(empty($unix_date)) { 15 return "Bad date"; 16 } 17 // is it future date or past date 18 if($now > $unix_date) { 19 $difference = $now - $unix_date; 20 $tense = "ago"; 21 22 } else { 23 $difference = $unix_date - $now; 24 $tense = "from now"; 25 } 26 27 for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { 28 $difference /= $lengths[$j]; 29 } 30 31 $difference = round($difference); 32 33 if($difference != 1) { 34 $periods[$j].= "s"; 35 } 36 37 return "$difference $periods[$j] {$tense}"; 38 }
使用上面函数即可 需要需要一下返回的信息
<?php $date = "2016-05-06 03:45"; $result = nicetime($date); // 1 days ago

浙公网安备 33010602011771号