循环数组以获得闰年php

时间:2017-10-11 01:26:53

标签: php loops for-loop

我是一个关于php的新手,我需要有关如何获取循环的帮助,例如for或foreach运行数组,然后定义数组中的哪一年是跳跃或不跳跃。

 $year = '2012,396,300,2000,1100,1089';
 $y2 = explode(',',$year);
 if (($year % 4 == 0) || ($year % 400 == 0 && $year % 4 != 0)) { 
    echo "True";
 } else{ 
    echo "False"; 
 }

感谢您的时间

2 个答案:

答案 0 :(得分:0)

将条件置于循环

if((leapyear%4==0 && leapyear%100!==0)||leapyear%400==0)
{
its a leap year
}

答案 1 :(得分:0)

试试这个,

 // this will check a year is leap year or not and return true/false
 function check_leap_year($year){
   return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
 }


 $years = '2012,396,300,2000,1100,1089';
 $y2 = explode(',',$years);

 foreach($y2 as $year){
 $test_leap_year = check_leap_year($year);
 if ($test_leap_year) { 
    echo "True";
  } else{ 
    echo "False"; 
 }
 }