2014-12-16 2 views
1

일년 중 각 월의 총 요일을 가져 오는 스크립트 함수를 만들려고합니다. 제 아이디어는 지난 12 개월의 보고서를 작성하는 것이고 윤년을 맞을 가능성이 있으며 계산에서이 문제를 다루지 않습니다. 2 월이 29 일인 윤년을 제외한 모든 달을 매핑하는 코드를 만들려고했습니다.윤년의 각 월의 총 일수를 얻는 방법

여기에 내 코드 :

public function checkMes(){ 

    // ----------------------------------- Verificar se o mes tem 30 ou 31 ou 28 dias ------------- 
    $month = array(); 
    for ($i = -10; $i < 0; $i++) 
    { 
     $check_month = date('m'); 

     if($check_month == '01' || $check_month == '03' || $check_month == '05' || $check_month == '07' || $check_month == '08' || $check_month == '10' || $check_month == '12') 
     { 
      $month[] = date('m/Y',strtotime('+'.$i.' months + 1 days')); 

     }elseif($check_month == '02'){ 
      $month[] = date('m/Y',strtotime('+'.$i.' months - 2 days')); 

     }else{ 
      $month[] = date('m/Y',strtotime('+'.$i.' months')); 

     } 
    } 
    return $month; 
} 

는 사람이 좀 도와 줄래?

[업데이트]

내 데이터를 계산하고보고해야하기 때문에 2 월 29 일 또는 28 일이 있는지 알 필요가

. 2 월 29 일에 판매 한 경우 2 월 28 일까지 내 보고서에서 고려해야합니다.

+1

'날짜 ('L')가, 그것은 윤년의 경우'을 알려줍니다. – AbraCadaver

+0

예상되는 결과는 무엇입니까? –

+1

'달 0 일'을 사용하여 지난 달의 마지막 날을 볼 수 있습니다. '2014 년 3 월 20 일 '은 실제로'2014 년 2 월 28 일'입니다. –

답변

0

.

Firts의를 얻기 위해 작은 함수를 만들 : 현재 연도가 100

에 의해 4 (400)과 나눌 수없는 경우로 나눌 경우 제가 몇 가지 스크립트 코드의 의미를 명확히합시다 계산하는 함수를 만듭니다 현재 연도를 계산하고 계산하십시오. 그것은이 같은 wseems :

public function checkMes(){ 
    $ano = date('Y'); //add this line 
    $this->leap($ano); // add this line 

    // ----------------------------------- Verificar se o mes tem 30 ou 31 ou 28 dias ------------- 
    $month = array(); 
    for ($i = -10; $i < 0; $i++) 
    { 
     $check_month = date('m'); 

     if($check_month == '01' || $check_month == '03' || $check_month == '05' || $check_month == '07' || $check_month == '08' || $check_month   == '10' || $check_month == '12') 
     { 
      $month[] = date('m/Y',strtotime('+'.$i.' months + 1 days')); 

     }elseif($check_month == '02'){ 

      if(isset($leap)){ //add this line 
       $month[] = date('m/Y',strtotime('+'.$i.' months - 1 days')); //add this line 

      }else{ //add this line 
       $month[] = date('m/Y',strtotime('+'.$i.' months - 2 days')); //add this line 

      } //add this line 

     }else{ 
      $month[] = date('m/Y',strtotime('+'.$i.' months')); 

     } 
    } 
    return $month; 
} 

그것은 당신에게 도움이되기를 바랍니다 :

public function leap($ano) 
{  
    $leap = false;  

    if ((($ano%4) == 0 && ($ano%100) != 0) || ($ano%400) == 0)  
    {  
     $leap = true;  
    }  
    return $leap; 
} 

지금 당신은 당신의 현재 기능을 수정하고 내가 주석에 다음 코드를 추가해야합니다. 내가 PHP에서 그것을 해결하기 위해 알고

+0

감사합니다. @bcesars. 이것은 정말로 도움이되었고 내 코드에서 완벽하게 작동합니다. –

1

date('t')은 주어진 달의 일수를 알려줍니다. 그래서 대신 IF-ELSEIF - 다른 블록의

http://php.net/manual/en/function.date.php

, 당신은 다만 수 : 당신은 date("t")을 사용할 수 있습니다

$month[] = date('m/Y',strtotime('+'.date('t').' days')); 
1

이 쉽게 하나입니다

<?php 
    $number_of_days_in_month = date('t'); 
?> 
0

, 당신의 조건은 잘못 :

이 시도 :

$arr = array(1944, 2011, 2012, 2013, 2014, 1986, 1800, 1900, 2000, 2056); 

    function leapYear($year){ 
     if(($year % 4) == 0 && ($year % 100) != 0){ 
      return true; 
     } 
     else{ 
      return false; 
     } 
    } 

    for($i = 0; $i < count($arr) ; $i++){ 
     $r = leapYear($arr[$i]); 

     if($r){ 
      echo 'Lepo year: ' . $arr[$i] . '<br>'; 
     }else{ 
      echo 'Not Leap year: ' . $arr[$i] . '<br>'; 
     } 

    } 
+1

이 역시 잘 작동합니다. 감사. –

관련 문제