2016-03-02 144 views
0

我想,因爲登錄的用戶的時間量的時間量PHP得到,因爲在幾秒鐘,幾分鐘或幾小時上次登錄

PHP:

$sql = "SELECT * FROM login WHERE username = '$user'" ; 
$result = mysqli_query($conn,$sql); 
$row = mysqli_fetch_assoc($result); 
$lastOnline = $row["lastOnline"];  
if ($lastOnline >= $time-60 or $lastOnline == $time){ 
    $lastOnline = "Now"; 
} else if ($lastOnline >= $time-3600){ 
    $lastOnline = date('i', $lastOnline-$time)." minutes ago"; 
} else if ($lastOnline >= $time-86400){ 
    $lastOnline = date('G', $lastOnline-$time)." hours ago"; 
} else if ($lastOnline >= $time-604800){ 
    $lastOnline = date('j', $lastOnline-$time)." days ago"; 
} 

的時間似乎是工作,但而不是分鐘。它似乎顯示相反。

+0

可以看到你們從數據庫中選擇? –

+0

_「時間似乎有效,但不是分鐘。」_數量是否錯誤?你有錯誤嗎?他們根本不顯示嗎?請澄清出了什麼問題。 – Epodax

+0

什麼是'$時間'? –

回答

2

你的計算是無效的,至少對於分鐘的情況下。

$lastOnline = date('i', $lastOnline-$time)." minutes ago"; 
         ^^^^^^^^^^^^^^^^^ 

,將永遠返回一個負的時間戳值,因爲LASTONLINE是在過去,時間是當前time()。它應該是另有

$lastOnline = date('i', $time-$lastOnline)." minutes ago"; 
         ^^^^^^^^^^^^^^^^^ 

Fiddle

$lastOnline=time()-600; 
$time=time(); 
$lastOnline = date('i', $time-$lastOnline)." minutes ago"; 
echo $lastOnline; 

輸出

10 minutes ago 
+0

時間和天數是否也錯了? – DrevanTonder

0
<?php 

/** 
* Get human readable time difference between 2 dates 
* 
* Return difference between 2 dates in year, month, hour, minute or second 
* The $precision caps the number of time units used: for instance if 
* $time1 - $time2 = 3 days, 4 hours, 12 minutes, 5 seconds 
* - with precision = 1 : 3 days 
* - with precision = 2 : 3 days, 4 hours 
* - with precision = 3 : 3 days, 4 hours, 12 minutes 
* 
* From: http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/ 
* 
* @param mixed $time1 a time (string or timestamp) 
* @param mixed $time2 a time (string or timestamp) 
* @param integer $precision Optional precision 
* @return string time difference 
*/ 
function get_date_diff($time1, $time2, $precision = 2) 
{ 
    // If not numeric then convert timestamps 
    if (!is_int($time1)) { 
     $time1 = strtotime($time1); 
    } 
    if (!is_int($time2)) { 
     $time2 = strtotime($time2); 
    } 

    // If time1 > time2 then swap the 2 values 
    if ($time1 > $time2) { 
     list($time1, $time2) = array(
      $time2, 
      $time1 
     ); 
    } 

    // Set up intervals and diffs arrays 
    $intervals = array(
     'year', 
     'month', 
     'day', 
     'hour', 
     'minute', 
     'second' 
    ); 
    $diffs  = array(); 

    foreach ($intervals as $interval) { 
     // Create temp time from time1 and interval 
     $ttime = strtotime('+1 ' . $interval, $time1); 
     // Set initial values 
     $add = 1; 
     $looped = 0; 
     // Loop until temp time is smaller than time2 
     while ($time2 >= $ttime) { 
      // Create new temp time from time1 and interval 
      $add++; 
      $ttime = strtotime("+" . $add . " " . $interval, $time1); 
      $looped++; 
     } 

     $time1   = strtotime("+" . $looped . " " . $interval, $time1); 
     $diffs[$interval] = $looped; 
    } 

    $count = 0; 
    $times = array(); 
    foreach ($diffs as $interval => $value) { 
     // Break if we have needed precission 
     if ($count >= $precision) { 
      break; 
     } 
     // Add value and interval if value is bigger than 0 
     if ($value > 0) { 
      if ($value != 1) { 
       $interval .= "s"; 
      } 
      // Add value and interval to times array 
      $times[] = $value . " " . $interval; 
      $count++; 
     } 
    } 

    // Return string with times 
    return implode(", ", $times); 
} 

/** 
Usage: 

$t = '2013-12-29T00:43:11+00:00'; 
$t2 = '2013-11-24 19:53:04 +0100'; 

var_dump(get_date_diff($t, $t2, 1)); // string '1 month' (length=7) 
var_dump(get_date_diff($t, $t2, 2)); // string '1 month, 4 days' (length=15) 
var_dump(get_date_diff($t, $t2, 3)); // string '1 month, 4 days, 5 hours' (length=24) 

*/ 

來源:https://gist.github.com/ozh/8169202

0

隨着DateTime你可以做這樣的事情(但是,它不會忽略零值)。

$then = new \DateTime($lastOnline); 
$now = new \DateTime(); 

$now->diff($then)->format('%m months, %d days, %h hours, %i minutes ago') 

或者一個更高級的版本,它會自動忽略零值

$then = new \DateTime($lastOnline); 
$now = new \DateTime(); 
$diff = $now->diff($then); 

$aliases = ["y"=>"years","m"=>"months", "d"=>"days", "h"=>"hours","i"=>"minutes","s"=>"seconds"]; 

echo implode(", ", array_filter(array_map(function($item) use ($diff, $aliases){ 
    if($diff->{$item} > 0) 
    { 
     return $diff->{$item} . " " . $aliases[$item]; 
    } 
},array_keys($aliases)))); 
相關問題