2017-05-21 1 views
1

반환해야하는 타임 스탬프가 있습니다.PHP 타임 스탬프 시간대

"1 시간 미만이면"30 분 전 "이라고 말해야합니다. 24 시간이 지나면 "6 시간 전"이라고 말하면됩니다. 24 시간이 지나면 "1 일 전"이라고 말하면됩니다. 48 시간이 지나면 "2 일 전"이라고 말하면됩니다.

조건문을 사용하면이 작업을 수행 할 수 있습니까?

$post_timestamp \t = strtotime($post_timestamp); 
 
$current_date = strtotime(time()); 
 
\t \t \t \t \t 
 
$datediff = $current_date - $post_timestamp; 
 
$days = floor($datediff/(60*60*24)); \t \t \t \t \t

+3

이 사용 사례를 위해 [Carbon] (http://carbon.nesbot.com/docs/)을 보는 것이 좋습니다. –

+0

Matt의 코멘트에 추가하면 찾고있는 방법은 diffForHumans() –

답변

2

난 당신이 DateTime, DateIntervalDatePeriod 사용할 수 있습니다 같아요 :

$date1 = new DateTime(); 
$date2 = DateTime::createFromFormat('U', $post_timestamp); # I assume a unix timestamp here 
//determine what interval should be used - 1 minute 
$interval = new \DateInterval('PT1M'); 
//create periods every minute between the two dates 
$periods = new \DatePeriod($date2, $interval, $date1); 
//count the number of objects within the periods 
$mins = iterator_count($periods); 


if ($mins < 60) 
{ 
    $say = "30 minutes ago"; 

} elseif ($mins >= 60 and $mins < 60 * 24) 
{ 
    $say = "6 hours ago"; 

} elseif ($mins >= 60 * 24 and $mins < 60 * 48) 
{ 
    $say = "1 day ago"; 

} elseif ($mins >= 60 * 48) 
{ 
    $say = "2 days ago"; 
} 

print $say; 
+1

고맙습니다. 거기에서 인쇄 할 분 단위의 시간과 일을 계산할 수 있습니다. –

+0

대단히 환영합니다 @SSAM –

1

당신은 같은 코드를 사용할 수 있습니다

지금까지 나는 일 수를 반환 할 수 있습니다 다음 :

date_default_timezone_set('Asia/Calcutta'); 
$post_timestamp="2017-05-21 5:00 pm"; 
$post_timestamp = strtotime($post_timestamp); 
$current_date = strtotime(date('Y-m-d h:i a'));     
$datediff = $current_date - $post_timestamp; 
$mins = ($datediff)/60; 
$hours = $datediff/(60 * 60); 

당신은 이것을 사용하여 분 및 시간을 얻을 것이며 그에 따라 당신의 상태를 놓으십시오.