2016-12-08 8 views
0

현재 가장 가까운 날짜를 기준으로 $showThis을 변경하려고합니다. 시차를 어떻게 비교할 수 있습니까? 현재 $interval1 > $interval2이 작동하지 않습니다. 나는 차이가 계산되고 있음을 알고 있지만 두 가지를 비교하는 방법을 모르겠습니다.PHP - 현재 날짜로부터 두 미래 날짜를 비교하십시오.

$currentTime = new DateTime("now"); 
$wednesday = new DateTime('wednesday 10:59pm'); 
$saturday = new DateTime('saturday 10:59pm'); 
$interval1 = $currentTime->diff($wednesday); 
$interval2 = $currentTime->diff($saturday); 

if($interval1 > $interval2){ 
    $showThis = $saturday->format('D m/d/Y h:i A'); 
} 
if($interval1 < $interval2){ 
    $showThis = $wednesday->format('D m/d/Y h:i A'); 
} 
+0

날짜 시간의 사랑하는 방법은 DateInterval http://php.net/manual/en/class 반환합니다. dateinterval.php – kyle

답변

1

getTimestamp 방법을 사용하십시오.

$interval1 = $wednesday->getTimestamp() - $currentTime->getTimestamp(); 
$interval2 = $saturday->getTimestamp() - $currentTime->getTimestamp(); 
0

당신이 그들을 비교하기 전에 0 날짜에 추가하여 초에 간격을 변환해야합니다 :

$currentTime = new DateTime("now"); 
$wednesday = new DateTime('wednesday 10:59pm'); 
$saturday = new DateTime('saturday 10:59pm'); 

$interval1 = $currentTime->diff($wednesday); 
$interval2 = $currentTime->diff($saturday); 

$seconds1 = date_create('@0')->add($interval1)->getTimestamp(); 
$seconds2 = date_create('@0')->add($interval2)->getTimestamp(); 

if ($seconds1 > $seconds2){ 
    $showThis = $saturday->format('D m/d/Y h:i A'); 
} 
else { 
    $showThis = $wednesday->format('D m/d/Y h:i A'); 
} 

echo $showThis;