这个小片段将为您提供从现在到给定日期之间的秒数差异.
原文链接:https://www.f2er.com/php/132935.html$dateString = "2011-11-03T17:27:56Z"; $date = strtotime($dateString); $diff = time() - $date; echo $diff;
function time_diff($s) { $m = 0; $hr = 0; $d = 0; $td = "now"; if ($s > 59) { $m = (int)($s/60); $s = $s-($m*60); // sec left over $td = "$m min"; } if ($m > 59) { $hr = (int)($m / 60); $m = $m - ($hr*60); // min left over $td = "$hr hr"; if ($hr > 1) { $td .= "s"; } if ($m > 0) { $td .= ",$m min"; } } if ($hr > 23) { $d = (int) ($hr / 24); $hr = $hr-($d*24); // hr left over $td = "$d day"; if ($d > 1) { $td .= "s"; } if ($d < 3) { if ($hr > 0) { $td .= ",$hr hr"; } if ($hr > 1) { $td .= "s"; } } } return $td; }
结合这两者就是你得到的:
$dateString = "2011-11-03T17:27:56Z"; $date = strtotime($dateString); $diff = time() - $date; echo time_diff($diff);
输出:
8 days