2010-04-22 189 views
26

我基本上試圖將Unix時間戳(time()函數)轉換爲與過去和未來日期兼容的相對日期/時間。所以輸出可以是:PHP:從時間戳產生相對日期/時間

兩週前

1小時和60分鐘前

15分鐘54秒前

10分鐘後15秒

首先,我試圖編碼這個,但做了一個巨大的不可維護的功能,然後我搜索了幾個小時的網絡,但我能找到的都是scri只產生一部分時間的小組(e.h:「1小時前」,不帶分鐘)。

你有腳本,已經這樣做?

+0

也許這可以幫助你:HTTP://計算器。 com/questions/2643113/convert-2010-04-16-163000-to-tomorrow-afternoon/2643137 – 2010-04-22 11:57:22

+1

可能的重複[如何計算relati (時間?)(http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time) – hakre 2011-11-05 22:42:09

回答

44

此功能爲您提供「1小時前」或「明天」,如'now'和'specific timestamp'之間的結果。

function time2str($ts) 
{ 
    if(!ctype_digit($ts)) 
     $ts = strtotime($ts); 

    $diff = time() - $ts; 
    if($diff == 0) 
     return 'now'; 
    elseif($diff > 0) 
    { 
     $day_diff = floor($diff/86400); 
     if($day_diff == 0) 
     { 
      if($diff < 60) return 'just now'; 
      if($diff < 120) return '1 minute ago'; 
      if($diff < 3600) return floor($diff/60) . ' minutes ago'; 
      if($diff < 7200) return '1 hour ago'; 
      if($diff < 86400) return floor($diff/3600) . ' hours ago'; 
     } 
     if($day_diff == 1) return 'Yesterday'; 
     if($day_diff < 7) return $day_diff . ' days ago'; 
     if($day_diff < 31) return ceil($day_diff/7) . ' weeks ago'; 
     if($day_diff < 60) return 'last month'; 
     return date('F Y', $ts); 
    } 
    else 
    { 
     $diff = abs($diff); 
     $day_diff = floor($diff/86400); 
     if($day_diff == 0) 
     { 
      if($diff < 120) return 'in a minute'; 
      if($diff < 3600) return 'in ' . floor($diff/60) . ' minutes'; 
      if($diff < 7200) return 'in an hour'; 
      if($diff < 86400) return 'in ' . floor($diff/3600) . ' hours'; 
     } 
     if($day_diff == 1) return 'Tomorrow'; 
     if($day_diff < 4) return date('l', $ts); 
     if($day_diff < 7 + (7 - date('w'))) return 'next week'; 
     if(ceil($day_diff/7) < 4) return 'in ' . ceil($day_diff/7) . ' weeks'; 
     if(date('n', $ts) == date('n') + 1) return 'next month'; 
     return date('F Y', $ts); 
    } 
} 
+0

但只返回小時與分鐘(例如),對吧? – KeyStroke 2010-04-22 12:41:32

+1

好嗎? http://pastie.org/929624 – osm 2010-04-22 12:46:00

13
function relativeTime($time) { 

    $d[0] = array(1,"second"); 
    $d[1] = array(60,"minute"); 
    $d[2] = array(3600,"hour"); 
    $d[3] = array(86400,"day"); 
    $d[4] = array(604800,"week"); 
    $d[5] = array(2592000,"month"); 
    $d[6] = array(31104000,"year"); 

    $w = array(); 

    $return = ""; 
    $now = time(); 
    $diff = ($now-$time); 
    $secondsLeft = $diff; 

    for($i=6;$i>-1;$i--) 
    { 
     $w[$i] = intval($secondsLeft/$d[$i][0]); 
     $secondsLeft -= ($w[$i]*$d[$i][0]); 
     if($w[$i]!=0) 
     { 
      $return.= abs($w[$i]) . " " . $d[$i][1] . (($w[$i]>1)?'s':'') ." "; 
     } 

    } 

    $return .= ($diff>0)?"ago":"left"; 
    return $return; 
} 

用法:

echo relativeTime((time()-256)); 
4 minutes 16 seconds ago 
9

這裏是我寫的東西。顯示相對於今天日期的過去日期。

/** 
* @param $date integer of unixtimestamp format, not actual date type 
* @return string 
*/ 
function zdateRelative($date) 
{ 
    $now = time(); 
    $diff = $now - $date; 

    if ($diff < 60){ 
     return sprintf($diff > 1 ? '%s seconds ago' : 'a second ago', $diff); 
    } 

    $diff = floor($diff/60); 

    if ($diff < 60){ 
     return sprintf($diff > 1 ? '%s minutes ago' : 'one minute ago', $diff); 
    } 

    $diff = floor($diff/60); 

    if ($diff < 24){ 
     return sprintf($diff > 1 ? '%s hours ago' : 'an hour ago', $diff); 
    } 

    $diff = floor($diff/24); 

    if ($diff < 7){ 
     return sprintf($diff > 1 ? '%s days ago' : 'yesterday', $diff); 
    } 

    if ($diff < 30) 
    { 
     $diff = floor($diff/7); 

     return sprintf($diff > 1 ? '%s weeks ago' : 'one week ago', $diff); 
    } 

    $diff = floor($diff/30); 

    if ($diff < 12){ 
     return sprintf($diff > 1 ? '%s months ago' : 'last month', $diff); 
    } 

    $diff = date('Y', $now) - date('Y', $date); 

    return sprintf($diff > 1 ? '%s years ago' : 'last year', $diff); 
} 
0

是Drupal的做它爲什麼不撕掉的方式 - http://api.drupal.org/api/drupal/includes%21common.inc/function/format_interval/7

<?php 
function format_interval($interval, $granularity = 2, $langcode = NULL) { 
    $units = array(
    '1 year|@count years' => 31536000, 
    '1 month|@count months' => 2592000, 
    '1 week|@count weeks' => 604800, 
    '1 day|@count days' => 86400, 
    '1 hour|@count hours' => 3600, 
    '1 min|@count min' => 60, 
    '1 sec|@count sec' => 1, 
); 
    $output = ''; 
    foreach ($units as $key => $value) { 
    $key = explode('|', $key); 
    if ($interval >= $value) { 
     $output .= ($output ? ' ' : '') . format_plural(floor($interval/$value), $key[0], $key[1], array(), array('langcode' => $langcode)); 
     $interval %= $value; 
     $granularity--; 
    } 

    if ($granularity == 0) { 
     break; 
    } 
    } 
    return $output ? $output : t('0 sec', array(), array('langcode' => $langcode)); 
} 
?> 

你可能不需要在t()的替代,你可以做你自己的事情format_plural很容易地爲你(可能)不需要支持多種語言。 http://api.drupal.org/api/drupal/includes%21common.inc/function/format_plural/7

+1

Drupal的功能是否受其版權/開源許可保護? – 2013-03-20 19:29:53

+0

@Rhino,完全取決於你打算如何處理你的代碼。如果你沒有發佈源代碼,你可以在Drupal中使用GPL代碼。如果你分發你的源代碼(爲了錢或免費),那麼使用Drupal的一部分會影響你可以重新分配的許可證(即你必須自己下發GPL許可證) - http:// drupal。組織/許可/ FAQ。 – 2013-05-19 12:01:31

+1

是的,我瞭解GPL,只是指出您可能想提及答案中的限制。 – 2013-05-20 09:40:39

4

我喜歡xdebug relativeTime函數。問題是我需要它有一定的粒度。

換句話說,如果我決定在幾秒或幾分鐘內停下來。 所以現在,

echo fTime(strtotime('-23 hours 5 minutes 55 seconds'),0); 

將顯示,

23小時,5分鐘前

代替

23小時,5分鐘,55幾秒前

我也希望它不會在陣列中降低,如果它達到較高的時間量之一。因此,如果顯示多年,我只想顯示幾年和幾個月。 所以現在,

echo fTime(strtotime('-1 year 2 months 3 weeks 4 days 16 hours 15 minutes 22 seconds'),0); 

將顯示

1年,2個月前

而不是

1年,2月,3周,4天,16小時,15分鐘,22秒前

以下代碼更改做了我所需要的。當然,道具先去xdebug。 希望別人可能會發現它有用:

function fTime($time, $gran=-1) { 

    $d[0] = array(1,"second"); 
    $d[1] = array(60,"minute"); 
    $d[2] = array(3600,"hour"); 
    $d[3] = array(86400,"day"); 
    $d[4] = array(604800,"week"); 
    $d[5] = array(2592000,"month"); 
    $d[6] = array(31104000,"year"); 

    $w = array(); 

    $return = ""; 
    $now = time(); 
    $diff = ($now-$time); 
    $secondsLeft = $diff; 
    $stopat = 0; 
    for($i=6;$i>$gran;$i--) 
    { 
     $w[$i] = intval($secondsLeft/$d[$i][0]); 
     $secondsLeft -= ($w[$i]*$d[$i][0]); 
     if($w[$i]!=0) 
     { 
      $return.= abs($w[$i]) . " " . $d[$i][1] . (($w[$i]>1)?'s':'') ." "; 
      switch ($i) { 
       case 6: // shows years and months 
        if ($stopat==0) { $stopat=5; } 
        break; 
       case 5: // shows months and weeks 
        if ($stopat==0) { $stopat=4; } 
        break; 
       case 4: // shows weeks and days 
        if ($stopat==0) { $stopat=3; } 
        break; 
       case 3: // shows days and hours 
        if ($stopat==0) { $stopat=2; } 
        break; 
       case 2: // shows hours and minutes 
        if ($stopat==0) { $stopat=1; } 
        break; 
       case 1: // shows minutes and seconds if granularity is not set higher 
        break; 
      } 
      if ($i===$stopat) { break 0; } 
     } 
    } 

    $return .= ($diff>0)?"ago":"left"; 
    return $return; 
} 

馬庫斯

3

我需要一個給我如下的結果,所以我寫我自己的。希望這會幫助某人。

用法示例:

$datetime = "2014-08-13 12:52:48"; 
echo getRelativeTime($datetime); //10 hours ago 
echo getRelativeTime($datetime, 1); //10 hours ago 
echo getRelativeTime($datetime, 2); //10 hours and 50 minutes ago 
echo getRelativeTime($datetime, 3); //10 hours, 50 minutes and 50 seconds ago 
echo getRelativeTime($datetime, 4); //10 hours, 50 minutes and 50 seconds ago 

代碼:

public function getRelativeTime($datetime, $depth=1) { 

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

    $plural = "s"; 
    $conjugator = " and "; 
    $separator = ", "; 
    $suffix1 = " ago"; 
    $suffix2 = " left"; 
    $now = "now"; 
    $empty = ""; 

    # DO NOT EDIT BELOW 

    $timediff = time()-strtotime($datetime); 
    if ($timediff == 0) return $now; 
    if ($depth < 1) return $empty; 

    $max_depth = count($units); 
    $remainder = abs($timediff); 
    $output = ""; 
    $count_depth = 0; 
    $fix_depth = true; 

    foreach ($units as $unit=>$value) { 
     if ($remainder>$value && $depth-->0) { 
      if ($fix_depth) { 
       $max_depth -= ++$count_depth; 
       if ($depth>=$max_depth) $depth=$max_depth; 
       $fix_depth = false; 
      } 
      $u = (int)($remainder/$value); 
      $remainder %= $value; 
      $pluralise = $u>1?$plural:$empty; 
      $separate = $remainder==0||$depth==0?$empty: 
          ($depth==1?$conjugator:$separator); 
      $output .= "{$u} {$unit}{$pluralise}{$separate}"; 
     } 
     $count_depth++; 
    } 
    return $output.($timediff<0?$suffix2:$suffix1); 
} 
+1

謝謝,我喜歡你的功能並使用它。 – phansen 2015-02-05 10:15:41

+1

不客氣:) @phansen – Ozzy 2015-02-07 17:32:48

+1

我也在我的項目中使用了這個稍微修改過的版本。 – VSG24 2016-09-08 19:58:16

1

下面是我用過去的時代:

function zdateRelative($date) 
{ 
    $diff = time() - $date; 
    $periods[] = [60, 1, '%s seconds ago', 'a second ago']; 
    $periods[] = [60*100, 60, '%s minutes ago', 'one minute ago']; 
    $periods[] = [3600*70, 3600, '%s hours ago', 'an hour ago']; 
    $periods[] = [3600*24*10, 3600*24, '%s days ago', 'yesterday']; 
    $periods[] = [3600*24*30, 3600*24*7, '%s weeks ago', 'one week ago']; 
    $periods[] = [3600*24*30*30, 3600*24*30, '%s months ago', 'last month']; 
    $periods[] = [INF, 3600*24*265, '%s years ago', 'last year']; 
    foreach ($periods as $period) { 
    if ($diff > $period[0]) continue; 
    $diff = floor($diff/$period[1]); 
    return sprintf($diff > 1 ? $period[2] : $period[3], $diff); 
    } 
} 
+1

非常感謝你。對於那些想要使用此功能,但僅限24小時的用戶,請在第三項[]中將70替換爲24。 – Bayou 2018-01-04 12:29:33