2016-05-29 31 views
0

我有這個時間戳php代碼,它不顯示正確的時間。它總是顯示8小時前的時間假設是幾分鐘前。將時間戳轉換爲PHP前的時間

/** 
 
    * Convert a timestap into timeago format 
 
    * @param time 
 
    * @return timeago 
 
    */ 
 

 
    public static function timeago($time, $tense = "ago"){ 
 
     if(empty($time)) return "n/a"; 
 
     $time=strtotime($time); 
 
     $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); 
 
     $lengths = array("60","60","24","7","4.35","12","10"); 
 
     $now = time(); 
 
     $difference = $now - $time; 
 
     for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { 
 
      $difference /= $lengths[$j]; 
 
     } 
 
     $difference = round($difference); 
 
     if($difference != 1) { 
 
      $periods[$j].= "s"; 
 
     } 
 
     return "$difference $periods[$j] $tense "; 
 
    }

我真的不明白爲什麼它不能正常工作

+0

什麼是輸入你期待什麼輸出? – naomik

+0

使用碳依賴,它會使你easyer你 –

回答

7

這裏是一個功能我爲你寫。

它使用DateTime class,所以PHP版本必須> = 5.3.0。

閱讀函數中的註釋以瞭解其工作原理。

function timeago($time, $tense='ago') { 
    // declaring periods as static function var for future use 
    static $periods = array('year', 'month', 'day', 'hour', 'minute', 'second'); 

    // checking time format 
    if(!(strtotime($time)>0)) { 
     return trigger_error("Wrong time format: '$time'", E_USER_ERROR); 
    } 

    // getting diff between now and time 
    $now = new DateTime('now'); 
    $time = new DateTime($time); 
    $diff = $now->diff($time)->format('%y %m %d %h %i %s'); 
    // combining diff with periods 
    $diff = explode(' ', $diff); 
    $diff = array_combine($periods, $diff); 
    // filtering zero periods from diff 
    $diff = array_filter($diff); 
    // getting first period and value 
    $period = key($diff); 
    $value = current($diff); 

    // if input time was equal now, value will be 0, so checking it 
    if(!$value) { 
     $period = 'seconds'; 
     $value = 0; 
    } else { 
     // converting days to weeks 
     if($period=='day' && $value>=7) { 
      $period = 'week'; 
      $value = floor($value/7); 
     } 
     // adding 's' to period for human readability 
     if($value>1) { 
      $period .= 's'; 
     } 
    } 

    // returning timeago 
    return "$value $period $tense"; 
} 

不要忘記設置時區你的工作

date_default_timezone_set('UTC'); 

使用它

echo timeago('1981-06-07'); // 34 years ago 
echo timeago(date('Y-m-d H:i:s')); // 0 seconds ago 

+0

謝謝,這確實解決了我的問題。 –

1

可能是你的時區設置不正確的!在獲取時刻之前,嘗試設置默認的時區:

date_default_timezone_set (string $timezone_identifier) 

More about the function on PHP.net

List of Supported Timezones

+0

這是爲時區'「時區」=> date_default_timezone_get(),' –

+0

@DarkSterix我不知道我明白你的意思。你寫的是獲取**時區的函數。我寫的是**設置**時區。如果你得到的時區和返回的時區是假的,那麼你得到的時間不同於你的時區。 –

+0

因爲該函數返回正確的時區,我不明白爲什麼它開始於8小時前,而不是1或2小時,或分鐘 –

0
function time_elapsed($datetime, $full = false) { 

$now = time(); 
$ago = strtotime($datetime); 

$diff = $now - $ago; 

$string = array(
    'year' => 31104000, 
    'month' => 2592000, 
    'week' => 604800, 
    'day' => 86400, 
    'hour' => 3600, 
    'minute'=> 60, 
    'second'=> 1 
); 

$data = array(); 

foreach ($string as $k => $v) { 

    if($diff > $v){ 
     $count = round($diff/$v); 
     $data[$k] = $count . (($count > 1) ? ' ' . $k .'s' : ' ' . $k); 
     $diff  = $diff % $v; 
    } 
} 

if (!$full) $data = array_slice($data, 0, 1); 
    return $data ? implode(', ', $data) . ' ago' : 'just now'; 
} 

echo time_elapsed('2016-01-18 13:07:30', true); 
     // 2 years, 1 month, 2 weeks, 6 days, 25 seconds ago 
echo time_elapsed('2016-01-18 13:07:30'); 
    // 2 years ago