2013-07-18 1 views
1

주어진 날짜별로 계절을 지정하겠습니다. 저는 4 계절이 날짜별로 (월별이 아님) 있습니다. 그래서 in_arrayrange()을 사용하기로 결정했지만 아무 것도 보여주지 않습니다.지정된 날짜 범위로 계절을 알려주는 방법

$p1=strtotime("2013-12-13"); 
$p2=strtotime("2014-02-20"); 
$h1a=strtotime("2014-02-21"); 
$h1b=strtotime("2014-04-31"); 
$l1=strtotime("2013-05-01"); 
$l2=strtotime("2013-10-31"); 
$h2a=strtotime("2013-11-01"); 
$h2b=strtotime("2013-12-19"); 


$today=strtotime(date("Y-m-d")); 

if(in_array($today, range($p1, $p2))){ 
    echo "peak"; 
}elseif(in_array($today, range($h1a, $h1b))){ 
    echo "hi1"; 
}elseif(in_array($today, range($l1, $l2))){ 
    echo "low"; 
}else(in_array($today, range($h2a, $h2b))){ 
    echo "h2"; 
} 

너희들이 내 코드를 개선 시겠어요 :

여기 내 코드입니다.

감사합니다.

+0

메모리 제한이 범위를 초과 했으므로 나는 범위 http://codepad.viper-7.com/PLFObE에서 단지 2 일을 사용했고 그것은 많은 출력을 보여준다. –

+0

현재 날짜를 시즌에 테스트하는 더 좋은 방법이 있습니까? – Wilf

+0

보다 크고 작음으로 답을 참조하십시오. –

답변

1

범위 때문에 메모리 한도가 초과되었습니다. 나는 범위 http://codepad.viper-7.com/PLFObE에서 단지 2 일을 사용했고 그것은 많은 출력을 보여준다.

보다 큰 값과 작은 값을 사용하여 날짜를 측정 할 수 있습니다.

if($today >= $p1 && $today <= $p2){ 
    echo "peak"; 
}elseif($today >= $h1a && $today <= $h1b){ 
    echo "hi1"; 
}elseif($today >= $l1 && $today <= $l2){ 
    echo "low"; 
}else($today >= $h2a && $today <= $h2b){ 
    echo "h2"; 
} 

편집

Codepad

+0

Yogesh, 코드가 멋지지만 작동하지 않습니다. 내가 기대했던대로 "낮은"것을 반환하지 않습니다. – Wilf

+1

@Wilf 편집 된 답변보기. 그냥 잘못된 순서로 비교를 사용했습니다. :) –

1

지금은 내 자신의 솔루션을 가지고있다. 그 재판에 감사드립니다. 코드는 다음과 같이 수정되었습니다. http://css-tricks.com/snippets/php/change-graphics-based-on-season/

<? 
function current_season() { 
     // Locate the icons 
     $icons = array(
       "peak" => "peak season", 
       "low" => "low season", 
       "high1" => "high1 season", 
       "high2" => "high2 season" 
     ); 

     // What is today's date - number 
     $day = date("z"); 

     // Days of peak 
     $peak_starts = date("z", strtotime("December 13")); 
     $peak_ends = date("z", strtotime("February 20")); 

     // Days of low 
     $low_starts = date("z", strtotime("May 1")); 
     $low_ends = date("z", strtotime("October 31")); 

     // Days of high 
     $high_starts = date("z", strtotime("February 21")); 
     $high_ends = date("z", strtotime("April 31")); 

     // If $day is between the days of peak, low, high, and winter 
     if($day >= $peak_starts && $day <= $peak_ends) : 
       $season = "peak"; 
     elseif($day >= $low_starts && $day <= $low_ends) : 
       $season = "low"; 
     elseif($day >= $high1_starts && $day <= $high1_ends) : 
       $season = "high"; 
     else : 
       $season = "high2"; 
     endif; 

     $image_path = $icons[$season]; 

     echo $image_path; 
} 
echo current_season(); 
?> 
관련 문제