2015-07-11 57 views
1

我有這個功能顯示友好日期TIMEAGO與UNIX時間戳和日期格式:PHP TIMEAGO友好的日期不行

function friendlyDate($timestamp, $formats = null) 
{ 

    $_DATE_FORMAT = array(
      'DAY'   => 'DAY', 
      'DAY_HOUR'  => 'DAY_HOUR', 
      'HOUR'   => 'HOUR', 
      'HOUR_MINUTE' => 'HOUR_MINUTE', 
      'MINUTE'  => 'MINUTE', 
      'MINUTE_SECOND' => 'MINUTE_SECOND', 
      'SECOND'  => 'SECOND', 
    ); 

    if ($formats == null) { 
     $formats = $_DATE_FORMAT; 
    } 

    $seconds = time() - $timestamp; 
    $minutes = floor($seconds/60); 
    $hours = floor($minutes/60); 
    $days = floor($hours/24); 

    if ($days > 0 && $days<=3) { 
     $diffFormat = 'DAY'; 
    } else if($days > 3){ 
     return date('Y-m-d',$timestamp); 
    } else { 
     $diffFormat = ($hours > 0) ? 'HOUR' : 'MINUTE'; 
     if ($diffFormat == 'HOUR') { 
      $diffFormat .= ($minutes > 0 && ($minutes - $hours * 60) > 0) ? '_MINUTE' : ''; 
     } else { 
      $diffFormat = (($seconds - $minutes * 60) > 0 && $minutes > 0) 
      ? $diffFormat.'_SECOND' : 'SECOND'; 
     } 
    } 

    $dateDiff = null; 
    switch ($diffFormat) { 
     case 'DAY': 
      $dateDiff = sprintf($formats[$diffFormat], $days); 
      break; 
     case 'DAY_HOUR': 
      $dateDiff = sprintf($formats[$diffFormat], $days, $hours - $days * 60); 
      break; 
     case 'HOUR': 
      $dateDiff = sprintf($formats[$diffFormat], $hours); 
      break; 
     case 'HOUR_MINUTE': 
      $dateDiff = sprintf($formats[$diffFormat], $hours, $minutes - $hours * 60); 
      break; 
     case 'MINUTE': 
      $dateDiff = sprintf($formats[$diffFormat], $minutes); 
      break; 
     case 'MINUTE_SECOND': 
      $dateDiff = sprintf($formats[$diffFormat], $minutes, $seconds - $minutes * 60); 
      break; 
     case 'SECOND': 
      $dateDiff = sprintf($formats[$diffFormat], $seconds); 
      break; 
    } 
    return $dateDiff; 
} 

echo friendlyDate在我的網頁是這樣的:

echo friendlyDate(1436613754,''); 

但我看到這個結果:SECOND

我能做些什麼來解決這個問題,我想顯示日期和時間。

+0

只是爲了確保它的清楚,你能[編輯]的問題,包括預期輸出? – IMSoP

+0

@IMSoP:輸出是:'SECOND'。我需要*秒前 – Perspolis

+0

就像我說的那樣,將它編入問題中,我的意思是所示示例的確切答案。 – IMSoP

回答

0

這是我自己的timeAgo()功能,我使用,

function timeAgo($timestamp) 
{ 
    //calculate here 
    //@type cast 
    //@current time, 
    //@difference in timestamp 

    $timestamp  = (int) $timestamp; 
    $current_time = $_SERVER['REQUEST_TIME']; 
    $diff   = $current_time - $timestamp; 

    //Intervals in seconds 
    $intervals = array (
      'year'  => 31556926, 
      'month'  => 2629744, 
      'week'  => 604800, 
      'day'  => 86400, 
      'hour'  => 3600, 
      'minute' => 60 
    ); 

    //Now we just find the difference 
    if($diff == 0) 
    { 
     return 'just now'; 
    } 

    if($diff < 60) 
    { 
     return $diff == 1 ? $diff.' second ago' : $diff.' seconds ago'; 
    } 

    if($diff >= 60 && $diff < $intervals['hour']) 
    { 
     $diff = floor($diff/$intervals['minute']); 
     return $diff == 1 ? $diff.' minute ago' : $diff.' minutes ago'; 
    } 

    if($diff >= $intervals['hour'] && $diff < $intervals['day']) 
    { 
     $diff = floor($diff/$intervals['hour']); 
     return $diff == 1 ? $diff.' hour ago' : $diff.' hours ago'; 
    } 

    if($diff >= $intervals['day'] && $diff < $intervals['week']) 
    { 
     $diff = floor($diff/$intervals['day']); 
     return $diff == 1 ? $diff.' day ago' : $diff.' days ago'; 
    } 

    if($diff >= $intervals['week'] && $diff < $intervals['month']) 
    { 
     $diff = floor($diff/$intervals['week']); 
     return $diff == 1 ? $diff.' week ago' : $diff.' weeks ago'; 
    } 

    if($diff >= $intervals['month'] && $diff < $intervals['year']) 
    { 
     $diff = floor($diff/$intervals['month']); 
     return $diff == 1 ? $diff.' month ago' : $diff.' months ago'; 
    } 

    if($diff >= $intervals['year']) 
    { 
     $diff = floor($diff/$intervals['year']); 
     return $diff == 1 ? $diff.' year ago' : $diff.' years ago'; 
    } 
} 

echo timeAgo(1436613754); //Outputs 8 months ago 
0

的主要問題,我可以看到的是,你沒有在你的格式字符串的任何格式的指令;即在$_DATE_FORMAT中的值。

sprintf()函數被執行到最後時,格式字符串只包含單詞「SECOND」。這意味着$seconds變量中的值被忽略。

嘗試的東西取代您的格式排列如下:

$_DATE_FORMAT = array(
    'DAY'   => '%d day(s)', 
    'DAY_HOUR'  => '%d day(s), %d hour(s)', 
    'HOUR'   => '%d hour(s)', 
    'HOUR_MINUTE' => '%d hour(s), %d minute(s)', 
    'MINUTE'  => '%d minute(s)', 
    'MINUTE_SECOND' => '%d minute(s), %d second(s)', 
    'SECOND'  => '%d second(s)' 
); 

sprintf()看到%d格式字符串,它替換從後續參數十進制整數值。在這種情況下,這是諸如$seconds等變量。這個例子並不完美,因爲它沒有給格式化單數和複數之間的選擇機會,但希望這是一個好的開始。

(這也許不是在功能上唯一的問題是我還沒有機會真正測試它。)