2014-07-19 7 views
0

나는 해결책을 찾고 있습니다. 슬라이드 쇼 내에 foreach 루프 (게시물)가 있고 게시 날짜 이후로 경과 된 시간을 검색해야합니다. 나는이 스레드를 따라 가고있다. PHP How to find the time elapsed since a date time? 이것은 지금까지 해왔 던 것이지만, 작동하지 않는다. 슬라이드 쇼를 부러 뜨렸거나, $ decolared 할 수없는 $ variabe에 관한 오류가있다. 누군가 도움을 줄 수 있습니까? 감사. 올바른 방향으로 날 가리 키도록 @Clément MaletPHP는 foreach 루프 내에서 날짜 시간 이후로 경과 된 시간을 찾습니다.

<?php if(count($comments) > 0): ?> 
<?php foreach($comments as $comment): ?> 
<?php 
$authorPicture = $comment['authorPicture']; 
$author = $comment['author']; 
$image = $comment['image']; 
$message = $comment['message']; 
$likes = $comment['likes']; 
$type = $comment['type']; 
$data = $comment['cacheDate']; 
$time = substr($data, 0, -3); //need to do this to retrieve a correct Unix timestamp 
function humanTiming ($time) 
{ 

    $time = time() - $time; // to get the time since that moment 
    $tokens = array (
     31536000 => 'year', 
     2592000 => 'month', 
     604800 => 'week', 
     86400 => 'day', 
     3600 => 'hour', 
     60 => 'minute', 
     1 => 'second' 
    ); 
    foreach ($tokens as $unit => $text) { 
     if ($time < $unit) continue; 
     $numberOfUnits = floor($time/$unit); 
     return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':''); 
    } 

    } 

?> 
<div class="data"><?php echo 'event happened '.humanTiming($time).' ago'; ?></div> 
<?php endforeach; ?> 
<?php else: ?> 
    <h2>There are no comments yet</h2> 
<?php endif; ?> 
+0

달성하려는 목표는 무엇입니까? DateTime :: diff는 당신을 도울지도 모른다 http://php.net/manual/en/datetime.diff.php – Sugar

+0

많이 감사합니다 !!! 나는이 예제를 발견했다. [look here !!!] (http://php.net/manual/en/datetime.diff.php#112368) – uomopalese

+0

이 예를 기반으로 어떻게 대시를 제거 할 수 있습니까? 검색된 날짜? 감사합니다 – uomopalese

답변

1

덕분에, DateTime::diff는 솔루션을 얻기 위해 나에게 도움이 : 게시 : 10시간 25 분

<?php foreach($comments as $comment): ?> 
<?php 
// foreach items 
$data = $comment['cacheDate']; // this store a unix value like 1404992204999 
// foreach items 
<!-- html code --> 
<div class="message"> 
<p class="data"><strong>Published: </strong> 
<?php 
    $date1 = $data; 
    $date2 = time(); 
    $subTime = $date1 - $date2; 
    $y = ($subTime/(60*60*24*365)); 
    $d = ($subTime/(60*60*24))%365; 
    $h = ($subTime/(60*60))%24; 
    $m = ($subTime/60)%60; 

    echo $h." hour and " . $m." minutes"; // no need for years and days 
?>  
<strong>ago </strong> 
</div> 
<?php endforeach; ?> 
<?php else: ?> 
<h2>There are no comments yet</h2> 
<?php endif; ?> 

과 최종 출력은 같다 전