2016-11-07 209 views
-1

我有一段時間在PHP函數,我用它來返回,如果用戶在線或用戶最後一次看到。如何解決Timeago錯誤

function tj_online_last($ptime) { 
    $estimate_time = time() - strtotime($ptime); 
    // if time diff is less than 1 minute then user is online 
    if ($estimate_time < 60) { 
     return 'online'; 
    } 
    $condition = array(
       12 * 30 * 24 * 60 * 60 => 'year', 
       30 * 24 * 60 * 60  => 'month', 
       24 * 60 * 60    => 'day', 
       60 * 60     => 'hour', 
       60      => 'minute', 
       1      => 'second', 
    ); 
    foreach($condition as $secs => $str) { 
     $d = $estimate_time/$secs; 
     if ($d >= 1) { 
      $r = round($d); 
      return $r.' '.$str.($r > 1 ? 's' : '').' ago'; 
     } 
    } 
} 

現在這裏的問題在這裏,當用戶第一次沒有登錄到他的帳戶的時間值是'NULL'。我想回到'永遠',而不是'48年前'令人討厭的,因爲我無法處理錯誤。

+0

然後'回「從來沒有」;',什麼是問題? –

回答

0

嘗試...... 當用戶第一次登錄,然後它的最後一項爲空,所以你需要檢查條件if截止日期爲空thenreturn 'never';

function tj_online_last($ptime) { 
    if(strtotime($ptime) <= 0){ 
     return 'never'; 
    } 
    $estimate_time = time() - strtotime($ptime); 
    // if time diff is less than 1 minute then user is online 
    if ($estimate_time < 60) { 
     return 'online'; 
    } 
    $condition = array(
       12 * 30 * 24 * 60 * 60 => 'year', 
       30 * 24 * 60 * 60  => 'month', 
       24 * 60 * 60    => 'day', 
       60 * 60     => 'hour', 
       60      => 'minute', 
       1      => 'second', 
    ); 
    foreach($condition as $secs => $str) { 
     $d = $estimate_time/$secs; 
     if ($d >= 1) { 
      $r = round($d); 
      return $r.' '.$str.($r > 1 ? 's' : '').' ago'; 
     } 
    } 
} 
+0

請詳細解釋答案,說明你的代碼如何工作以及他犯了什麼錯誤。 –

+0

謝謝先生,正是我所需要的,它按預期工作。我從來沒有這樣想過 –

+0

您的歡迎..;) –