2012-09-09 3 views
2

I이 등time_since 기능 표시 -1 년

<?php 

/* Works out the time since the entry post, takes a an argument in unix time (seconds) */ 
function time_since($original) { 
    // array of time period chunks 
    $chunks = array(
     array(60 * 60 * 24 * 365 , 'year'), 
     array(60 * 60 * 24 * 30 , 'month'), 
     array(60 * 60 * 24 * 7, 'week'), 
     array(60 * 60 * 24 , 'day'), 
     array(60 * 60 , 'hour'), 
     array(60 , 'minute'), 
    ); 

    $today = time(); /* Current unix time */ 
    $since = $today - $original; 

    // $j saves performing the count function each time around the loop 
    for ($i = 0, $j = count($chunks); $i < $j; $i++) { 

     $seconds = $chunks[$i][0]; 
     $name = $chunks[$i][1]; 

     // finding the biggest chunk (if the chunk fits, break) 
     if (($count = floor($since/$seconds)) != 0) { 
      // DEBUG print "<!-- It's $name -->\n"; 
      break; 
     } 
    } 

    $print = ($count == 1) ? '1 '.$name : "$count {$name}s"; 

    if ($i + 1 < $j) { 
     // now getting the second item 
     $seconds2 = $chunks[$i + 1][0]; 
     $name2 = $chunks[$i + 1][1]; 

     // add second item if it's greater than 0 
     if (($count2 = floor(($since - ($seconds * $count))/$seconds2)) != 0) { 
      $print .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s"; 
     } 
    } 
    return $print; 
} 

echo time_since(1347216222); 

?> 

출력에 .. 일이다 타임 스탬프 1347216222 다음 그것이 시간에 얼마나 오래 전에 확인하는 time_since 기능을 사용하고 분 -1 years, 12 months입니다. 누구든지이 문제를 해결하도록 도와 줄 수 있습니까?

+1

봐 : http://stackoverflow.com/questions/5010016/php-time-since-function – user973254

+5

어제 2 번 그런 질문에 대답했습니다 ... –

+0

http://codepad.org/JXZ 7as0L – Jake

답변

0

DateTimeDateInterval을 사용하는 기능을 맞춤 설정하여 더 읽기 쉽게 만들어졌습니다.

시간 차이가 어떻게 처리되는지에 관해 게시 한 기능과 다릅니다. 귀하의 경우에는 값을 두 번 테스트했습니다 (각 "단위"에 한 번씩). 이렇게하려면 복잡한 계산이 필요했습니다.

아래의 기능은 DateInterval을 사용하면 몇 년, 나방, 일, 시간, 분 및 초 (예 : 2015 년에서 2014 년 사이의 차이는 사용할 수 있음)를 즉시 사용할 수 있다는 사실을 이용합니다. 1 년 및 0 초, UNIX 타임 스탬프와 반대). 또한 DateTime을 사용하면 시간대를 정상적으로 처리 할 수 ​​있습니다.

그 차이점을 인쇄하는 방법 만 신경 써야합니다. 이것은 정확히 for 루프입니다. 우리는 두 개의 값을 추출하고 추가 항목 (우리는 이러한 항목 보호자라고 부름)을 사용합니다. 두 번째 항목을 추출 할 때 추가 조건을 저장하십시오.

<?php 
function pluralize($number, $unit) { 
    return $number . ' ' . $unit . ($number !== 1 ? 's' : ''); 
} 

function time_since(DateTime $original) { 
    $now = new DateTime('now'); 
    $diff = $now->diff($original); 

    //is from the past? 
    if ($diff->invert) { 
     $chunks = [ 
      [$diff->y, 'year'], 
      [$diff->m, 'month'], 
      [$diff->d, 'day'], 
      [$diff->h, 'hour'], 
      [$diff->i, 'minute'], 
      [$diff->s, 'second'], 
      [0, 'guardian'], 
     ]; 

     for ($i = 0; $i < count($chunks) - 1; $i ++) { 
      list ($value, $unit) = $chunks[$i]; 
      if ($value !== 0) { 
       $text = pluralize($value, $unit); 

       //append next unit as well, if it's available and nonzero 
       list ($nextValue, $nextUnit) = $chunks[$i + 1]; 
       if ($nextValue !== 0) { 
        $text .= ' ' . pluralize($nextValue, $nextUnit); 
       } 

       return $text; 
      } 
     } 
    } 

    return 'just now'; 
} 

그리고 시험 :

echo time_since(new DateTime('2014-01-01')) . PHP_EOL; 
echo time_since(new DateTime('2014-10-09')) . PHP_EOL; 
echo time_since(new DateTime('2014-11-09')) . PHP_EOL; 
echo time_since(new DateTime('2014-11-10')) . PHP_EOL; 
echo time_since(new DateTime('2014-11-10 13:05')) . PHP_EOL; 
echo time_since(new DateTime('2114-11-10 13:05')) . PHP_EOL; //future 

결과 : 여기

이 함수의

이 질문에
[email protected]:~$ php test.php 
10 months 9 days 
1 month 1 day 
1 day 13 hours 
13 hours 5 minutes 
38 seconds 
just now 
관련 문제