Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

php 星期日的个数(php输入年份月份输出天数)

作者:殘月殺手   发布日期:2025-06-16   浏览:93

以下是一个PHP函数,它可以根据输入的年份和月份输出该月份中星期日的个数:

function countSundays($year, $month) {
    $firstDay = date('Y-m-01', strtotime($year . '-' . $month . '-01'));
    $lastDay = date('Y-m-t', strtotime($firstDay));

    $sundays = 0;
    $currentDay = $firstDay;

    while ($currentDay <= $lastDay) {
        if (date('N', strtotime($currentDay)) == 7) {
            $sundays++;
        }

        $currentDay = date('Y-m-d', strtotime($currentDay . ' +1 day'));
    }

    return $sundays;
}

// 示例用法
$year = 2022;
$month = 3;
$sundays = countSundays($year, $month);
echo "在" . $year . "年" . $month . "月中,有" . $sundays . "个星期日。";

请注意,此函数假设输入的年份和月份是有效的。如果输入的年份和月份无效,例如月份小于1或大于12,函数可能会产生不正确的结果。

上一篇:php移除重复数组(php反转一个数组)

下一篇:php 输出文本换行(java输出换行)

大家都在看

php session用法

phpisset函数

php后端

php爬虫框架

php读取csv文件

php 三元表达式

php文件加密

php 拆分字符串

php pcntl

php ||

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站