2016-02-12 82 views
0

我有一個函數返回多久以前的時間發佈,但似乎有一個小問題。第一次,$ timeAgo1工作得很好,但第二次,$ timeAgo2似乎返回負秒。這是怎麼發生的?TimeAgo函數返回負秒

<?php 

//*****************************************************START OF FUNCTION 
function timeAgo($time_ago) { 

    $time_ago = strtotime($time_ago); 
    $cur_time = time(); 
    $time_elapsed = $cur_time - $time_ago; 
    $seconds = $time_elapsed ; 
    $minutes = round($time_elapsed/60); 
    $hours = round($time_elapsed/3600); 
    $days = round($time_elapsed/86400); 
    $weeks = round($time_elapsed/604800); 
    $months = round($time_elapsed/2600640); 
    $years = round($time_elapsed/31207680); 

    echo $seconds."<br>"; 
    if ($seconds <= 60) { 
     return "just now"; 
    } //end of if ($seconds <= 60) 

    else if ($minutes <=60) { 
     if ($minutes == 1) { 
      return "one minute ago"; 
     } //end of else if ($minutes <=60) 
     else { 
      return "$minutes minutes ago"; 
     } //end of else not ($minutes == 1) 
    } //end of else if ($minutes <= 60) 


    else if ($hours <= 24) { 
     if ($hours == 1) { 
      return "an hour ago"; 
     } //end of if ($hours == 1) 
     else { 
      return "$hours hours ago"; 
     } //end of else not ($hours == 1) 
    } //end of else if ($hours <= 24) 


    else if ($days <= 7) { 
     if ($days == 1) { 
      return "yesterday"; 
     } //end of else if ($days <= 7) 
     else { 
      return "$days days ago"; 
     } //end of else not ($days == 1) 
    } //end of else if ($days <= 7) 

    else if ($weeks <= 4.3) { 
     if ($weeks == 1) { 
      return "a week ago"; 
     } //end of if ($weeks == 1) 
     else { 
      return "$weeks weeks ago"; 
     } //end of else not ($weeks == 1) 
    } //end of else if ($weeks <= 4.3) 


    else if ($months <= 12) { 
     if ($months == 1) { 
      return "a month ago"; 
     } //end of if ($months == 1) 
     else { 
      return "$months months ago"; 
     } //end of else not ($months == 1) 
    } //end of else if ($months <= 12) 

    else { 
     if ($years == 1) { 
      return "one year ago"; 
     } //end of if ($years == 1) 
     else { 
      return "$years years ago"; 
     } //end of else not ($years == 1) 
    } //end of last else 

} //end of function timeAgo($time_ago) 
//*****************************************************END OF FUNCTION 

$date1 = "2016-02-10"; 
$time1 = "22:41:58"; 
$date2 = "2016-02-12"; 
$time2 = "15:25:57"; 

$timeAgo1 = timeAgo($date1.$time1); 
$timeAgo2 = timeAgo($date2.$time2); 

echo $timeAgo1."<br>".$timeAgo2; 

?> 

回答

0

這可能是一些與您的服務器,因爲我用2個不同的服務器,結果

166733 
20094 
2 days ago 
6 hours ago 

這可能是你的服務器位於其他地方比你,所以time();功能展示與您所期望的不同的時間戳。

第二TIMEAGO的時間戳是1455290705讓你的服務器可能位於某處它在哪裏/在超過1455290705.