Home >Backend Development >PHP Problem >PHP determines whether it is a leap year

PHP determines whether it is a leap year

尚
Original
2019-10-29 13:14:2011329browse

PHP determines whether it is a leap year

1, PHP leap year calculation method 1:

The leap year is when the remainder of 4 is 0, the remainder of 100 is not equal to 0, and the remainder of 400 is equal to 0 The year is a leap year.

$day = date('Y');
if ($day%4==0&&($day%100!=0 || $day%400==0)){
    echo $day.'是闰年';
}else{
    echo $day.'不是闰年';
}

2, PHP method two to determine leap year:

date('L'); you can directly determine whether the current year is a leap year, return 1 if true, otherwise return 0.

$year = 2008;//可以像上例一样用mt_rand随机取一个年,也可以随便赋值。
$time = mktime(20,20,20,4,20,$year);//取得一个日期的 Unix 时间戳;
if (date("L",$time)==1){ //格式化时间,并且判断是不是闰年,后面的等于一也可以省略;
echo $year."是闰年";
}else{
echo $year."不是闰年";
}

3, PHP’s third method of judging leap years and calculating leap years:

$year = 2000;
$time = mktime(20,20,20,2,1,$year);//取得一个日期的 Unix 时间戳;
if (date("t",$time)==29){ //格式化时间,并且判断2月是否是29天;
echo $year."是闰年";//是29天就输出时闰年;
}else{
echo $year."不是闰年";
}

Recommendation: php server

The above is the detailed content of PHP determines whether it is a leap year. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn