2011-04-08 19 views
0

我有一個腳本,用於檢查日期和時間條目已添加到數據庫,將它與今天的日期和時間進行比較,並輸出最相關單位(分鐘,小時,天) ,周等)。計算時差的PHP問題

問題是,超過一週的任何條目僅顯示爲「6天前添加」。

我明顯有一些問題,我計算時差的方式,但我似乎無法識別它。

這裏是我的代碼:

$date_time_added = $date_added . $time_added; 
    $current_date_time = $current_date . $current_time; 

    // Check how many X ago comment was added 
    $diff = abs(strtotime($current_date_time) - strtotime($date_time_added)); 
    $years = floor($diff/(365*60*60*24)); 
    $months = floor(($diff - $years * 365*60*60*24)/(30*60*60*24)); 
    $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); 
    $hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));  
    $minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60); 
    $seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60)); 
    // Check if comment was added in last 24 hours 
    if ($days < '1') { 
     // Check if comment was added in the last hour 
     if($hours == 0) { 
      // Check if comment wass added in the last minute 
      if ($minutes == 0) { 
       $when = 'Posted ' . $seconds . ' seconds ago'; 
       } else { 
        $when = 'Posted ' . $minutes . ' minutes ago';    } 
       } else { 
        $when = 'Posted ' . $hours . ' hours ago'; 
      } 
    } else { 
     $when = 'Posted ' . $days . ' days ago'; 
    } 

感謝您的幫助。

+0

什麼格式是日期/時間?我需要他們做一個測試用例:) – Khez 2011-04-08 10:08:35

+0

日期是Y-m-d,時間是H:我:S。 – 2011-04-08 10:10:01

+1

並請定義'$ yearInSeconds = 365 * 60 * 60 * 24;'並在每次需要時使用它;-) – Capsule 2011-04-08 10:18:54

回答

1

爲什麼在計算月份時多少年?如果你只需要最相關的單位也不會這樣去做:

$date_time_added = $date_added . $time_added; 
$current_date_time = $current_date . $current_time; 

// Check how many X ago comment was added 
$diff = abs(strtotime($current_date_time) - strtotime($date_time_added)); 
$years = floor($diff/(365*60*60*24)); 
$months = floor($diff/(30*60*60*24)); 
$days = floor($diff/(60*60*24)); 
$hours = floor($diff/(60*60));  
$minutes = floor($diff/60); 
$seconds = $diff; 

if($years==0) 
{ 
    if($months==0) 
    { 
     if($days==0) 
     { 
      if($hours==0) 
      { 
       if($minutes==0) 
       { 
        $when='Posted '.$seconds.' seconds ago'; 
       } 
       else 
       { 
        $when='Posted '.$minutes.' minutes ago'; 
       } 
      } 
      else 
      { 
       $when='Posted '.$hours.' hours ago'; 
      } 
     } 
     else 
     { 
      $when='Posted '.$days.' days ago'; 
     } 
    } 
    else 
    { 
     $when='Posted '.$days.' months ago'; 
    } 
} 
else 
{ 
    $when='Posted '.$years.' years ago'; 
} 
+0

這工作完美。謝謝! – 2011-04-08 13:01:35

1

floor()更改爲round()。另外,在上次作業中拼寫錯誤$minutes

+0

謝謝,我沒有發現拼寫。阿爾索斯之前並沒有來過()。 – 2011-04-08 13:01:17

0

我結束了使用加雷思的答案在我的項目之一。這是一個修改後的版本,會給你1天或1個月,而不是1天或1個月。謝謝Gareth!

$diff = (strtotime("Now") - strtotime($date_time_added)); 
           $years = floor($diff/(365*60*60*24)); 
           $months = floor($diff/(30*60*60*24)); 
           $days = floor($diff/(60*60*24)); 
           $hours = floor($diff/(60*60));  
           $minutes = floor($diff/60); 
           $seconds = $diff; 

           if($years==0) 
           { 
            if($months==0) 
            { 
             if($days==0) 
             { 
              if($hours==0) 
              { 
               if($minutes==0) 
               { 
                $when = ($seconds != 1) ? $seconds . ' seconds ago' : $seconds . ' second ago'; 
               } 
               else 
               { 
                $when = ($minutes != 1) ? $minutes . ' minutes ago' : $minutes . ' minute ago'; 
               } 
              } 
              else 
              { 
               $when = ($hours != 1) ? $hours . ' hours ago' : $hours . ' hour ago' ; 
              } 
             } 
             else 
             { 
              $when = ($days != 1) ? $days . ' days ago' : $days . ' day ago' ; 
             } 
            } 
            else 
            { 
             $when = ($months != 1) ? $months . ' months ago' : $months . ' months ago' ; 
            } 
           } 
           else 
           { 
            $when = ($years != 1) ? $years . ' years ago' : $years . ' years ago' ; 
           }