找到闰年

mal*_*lly 1 php for-loop

我正在使用PHP代码,开始我正在使用for statment.

for($i=0; $i<4; $i++)
Run Code Online (Sandbox Code Playgroud)

现在我明白$ i = 0从0开始计数而$ i <$ 4意味着从0到低于4.

我想要实现的是为语句添加多个而不是使用多个PHP代码.

for($i=0; $i<4; $i++)
for($i=4; $i<8; $i++)
for($i=8; $i<12; $i++)
for($i=12; $i<16; $i++)
Run Code Online (Sandbox Code Playgroud)

.......等等,以便列出所有结果.

<?php
$day = "";
for($i=0; $i<4; $i++)
{
    $day =  date("d", mktime(0, 0, 0, 2, 29, date("Y")+$i));
    if($day == 29)
    {
        $year = date("Y")+$i;
        break;
    }
}
echo "<p>The next leap year is 29th February $year</p>";    
?>
Run Code Online (Sandbox Code Playgroud)

回声结果将是:

下一个闰年是2016年2月29日

下一个闰年是2020年2月29日

Dev*_*er0 9

您可以查看闰年 date("L")

$yearsToCheck = range(2013, 2020);

foreach ($yearsToCheck as $year) {
    $isLeapYear = (bool) date('L', strtotime("$year-01-01"));
    printf(
        '%d %s a leap year%s',
        $year,
        $isLeapYear ? 'is' : 'is not',
        PHP_EOL
    );
}
Run Code Online (Sandbox Code Playgroud)

产量

2013 is not a leap year
2014 is not a leap year
2015 is not a leap year
2016 is a leap year
2017 is not a leap year
2018 is not a leap year
2019 is not a leap year
2020 is a leap year
Run Code Online (Sandbox Code Playgroud)


Den*_*her 7

使用闰年条件,只检查年份.为此使用一个声明!

function is_leap_year($year)
{
   return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
}
Run Code Online (Sandbox Code Playgroud)