溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php如何實(shí)現(xiàn)用已經(jīng)過去多長時(shí)間的方式顯示時(shí)間

發(fā)布時(shí)間:2021-06-25 14:50:48 來源:億速云 閱讀:180 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了php如何實(shí)現(xiàn)用已經(jīng)過去多長時(shí)間的方式顯示時(shí)間,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

具體如下:

這里以一種可讀性比較好的方式顯示已經(jīng)過去多長時(shí)間,比如:距離現(xiàn)在10秒,距離現(xiàn)在1天等等。

function time_is_older_than($t, $check_time){
  $t = strtolower($t);
  $time_type = substr(preg_replace('/[^a-z]/', '', $t), 0, 1);
  $val = intval(preg_replace('/[^0-9]/', '', $t));
  $ts = 0;
  // (s)econds, (m)inutes, (d)ays, (y)ears
  if ($time_type == 's'){ $ts = $val; }
  else if ($time_type == 'm'){ $ts = $val * 60; }
  else if ($time_type == 'h'){ $ts = $val * 60 * 60; }
  else if ($time_type == 'd'){ $ts = $val * 60 * 60 * 24; }
  else if ($time_type == 'y'){ $ts = $val * 60 * 60 * 24 * 365; }
  else { die('Unknown time format given!'); }
  if ($check_time < (time()-$ts)){ return true; }
  return false;
}

//使用范例:
// timestamp to test: 
// (could be from an database or something else)
$time = 1146722922;
// long if check:
if (time_is_older_than('30m', $time)){
  print 'The given timestamp: ' . date('l dS \of F Y h:i:s A',$time);
  print " - is older than 30 minutes<br/>\n";
}
else {
  print 'The given timestamp: ' . date('l dS \of F Y h:i:s A',$time);
  print " - is NOT older than 30 minutes<br/>\n";
}
// short checks:
if (time_is_older_than('10s', $time)){ print "Is older than 10 seconds<br/>\n"; }
if (time_is_older_than('200m', $time)){ print "Is older than 200 minutes<br/>\n"; }
if (time_is_older_than('2h', $time)){ print "Is older than 2 hours<br/>\n"; }
if (time_is_older_than('4d', $time)){ print "Is older than 4 days<br/>\n"; }
if (time_is_older_than('1y', $time)){ print "Is older than one year<br/>\n"; }

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“php如何實(shí)現(xiàn)用已經(jīng)過去多長時(shí)間的方式顯示時(shí)間”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI