当前位置: 首页 > news >正文

PHP 生成当月日期

 一:按日期顺序排列的数组,而不是按周分组的二维数组


/*日期生成
*day: 日期数字
*date: 完整的日期字符串 (YYYY-MM-DD)
*is_current_month: 是否属于当前月份
*is_prev_month: 是否是上个月的日期
*is_next_month: 是否是下个月的日期
*is_today: 是否是今天
*/
function generateFullCalendar($year = null, $month = null) {// 如果没有提供年份和月份,则使用当前年月$year = $year ?? date('Y');$month = $month ?? date('m');// 获取本月的第一天$firstDayOfMonth = date('Y-m-01', strtotime("$year-$month-01"));// 获取本月的第一天是星期几(0-6,0表示周日)$firstDayOfWeek = date('w', strtotime($firstDayOfMonth));// 获取本月的总天数$daysInMonth = date('t', strtotime($firstDayOfMonth));// 获取上个月的天数$prevMonthDays = date('t', strtotime('-1 month', strtotime($firstDayOfMonth)));// 计算需要显示的上个月日期(补全第一周)$calendar = [];for ($i = $firstDayOfWeek - 1; $i >= 0; $i--) {$day = $prevMonthDays - $i;$prevMonth = date('m', strtotime('-1 month', strtotime($firstDayOfMonth)));$prevYear = date('Y', strtotime('-1 month', strtotime($firstDayOfMonth)));$calendar[] = ['day' => $day,'date' => sprintf("%04d-%02d-%02d", $prevYear, $prevMonth, $day),'is_current_month' => false,'is_prev_month' => true,'is_next_month' => false,'is_today' => false];}// 添加本月的日期for ($day = 1; $day <= $daysInMonth; $day++) {$date = sprintf("%04d-%02d-%02d", $year, $month, $day);$calendar[] = ['day' => $day,'date' => $date,'is_current_month' => true,'is_prev_month' => false,'is_next_month' => false,'is_today' => ($date == date('Y-m-d'))];}// 计算需要显示的下个月日期(只补全最后一周)$totalDaysDisplayed = count($calendar);$daysNeededToCompleteLastWeek = (7 - ($totalDaysDisplayed % 7)) % 7;if ($daysNeededToCompleteLastWeek > 0) {for ($day = 1; $day <= $daysNeededToCompleteLastWeek; $day++) {$nextMonth = date('m', strtotime('+1 month', strtotime($firstDayOfMonth)));$nextYear = date('Y', strtotime('+1 month', strtotime($firstDayOfMonth)));$calendar[] = ['day' => $day,'date' => sprintf("%04d-%02d-%02d", $nextYear, $nextMonth, $day),'is_current_month' => false,'is_prev_month' => false,'is_next_month' => true,'is_today' => false];}}return $calendar;
}

 二:按周分组的二维数组

/*日期生成
*day: 日期数字
*date: 完整的日期字符串 (YYYY-MM-DD)
*is_current_month: 是否属于当前月份
*is_prev_month: 是否是上个月的日期
*is_next_month: 是否是下个月的日期
*is_today: 是否是今天
*/
function generateFullCalendar($year = null, $month = null) {// 如果没有提供年份和月份,则使用当前年月$year = $year ?? date('Y');$month = $month ?? date('m');// 获取本月的第一天和最后一天$firstDayOfMonth = date('Y-m-01', strtotime("$year-$month-01"));$lastDayOfMonth = date('Y-m-t', strtotime("$year-$month-01"));// 获取本月的第一天是星期几(0-6,0表示周日)$firstDayOfWeek = date('w', strtotime($firstDayOfMonth));// 获取本月的总天数$daysInMonth = date('t', strtotime($firstDayOfMonth));// 获取上个月的天数$prevMonthDays = date('t', strtotime('-1 month', strtotime($firstDayOfMonth)));// 计算需要显示的上个月日期(补全第一周)$prevMonthDates = [];for ($i = $firstDayOfWeek - 1; $i >= 0; $i--) {$day = $prevMonthDays - $i;$prevMonth = date('m', strtotime('-1 month', strtotime($firstDayOfMonth)));$prevYear = date('Y', strtotime('-1 month', strtotime($firstDayOfMonth)));$prevMonthDates[] = ['day' => $day,'date' => sprintf("%04d-%02d-%02d", $prevYear, $prevMonth, $day),'is_current_month' => false,'is_prev_month' => true,'is_next_month' => false];}// 计算本月的日期$currentMonthDates = [];for ($day = 1; $day <= $daysInMonth; $day++) {$currentMonthDates[] = ['day' => $day,'date' => sprintf("%04d-%02d-%02d", $year, $month, $day),'is_current_month' => true,'is_prev_month' => false,'is_next_month' => false,'is_today' => ($day == date('j') && $month == date('m') && $year == date('Y'))];}// 计算需要显示的下个月日期(只补全最后一周)$totalDaysDisplayed = count($prevMonthDates) + $daysInMonth;$daysNeededToCompleteLastWeek = (7 - ($totalDaysDisplayed % 7)) % 7;$nextMonthDates = [];if ($daysNeededToCompleteLastWeek > 0) {for ($day = 1; $day <= $daysNeededToCompleteLastWeek; $day++) {$nextMonth = date('m', strtotime('+1 month', strtotime($firstDayOfMonth)));$nextYear = date('Y', strtotime('+1 month', strtotime($firstDayOfMonth)));$nextMonthDates[] = ['day' => $day,'date' => sprintf("%04d-%02d-%02d", $nextYear, $nextMonth, $day),'is_current_month' => false,'is_prev_month' => false,'is_next_month' => true];}}// 合并所有日期$allDates = array_merge($prevMonthDates, $currentMonthDates, $nextMonthDates);// 按周分组(确保每周7天)$calendar = array_chunk($allDates, 7);return $calendar;
}

http://www.lqws.cn/news/446617.html

相关文章:

  • 解决 Docker 里 DrissionPage 无法连接浏览器的问题,内含直接可用的Docker镜像(DrissionPage 浏览器链接失败 怎么办?)
  • 23种设计模式--简单工厂模式理解版
  • 日本生活:日语语言学校-日语作文-沟通无国界(3)-题目:わたしの友達
  • 基于 Web 的 3D 设计工具Spline介绍
  • 理解服务注册与发现
  • DeserializationViewer使用说明
  • java IO流
  • Git vs Perforce P4:版本控制系统选型指南(附适用场景、团队类型)
  • 【嵌入式】鲁班猫玩法大全
  • LVDS接口
  • 华为网路设备学习-25(路由器OSPF - 特性专题 二)
  • vscode设置代码字体
  • repo 工具
  • 行业热点丨手机中框设计如何体现增材思维?
  • 计算机导论期末快速复习指南
  • “本地化思维+模块化体验”:一款轻量数据中心监控系统的真实测评
  • 软件测试基础知识(一)
  • StableDiffusion实战-手机壁纸制作 第一篇:从零基础到生成艺术品的第一步!
  • API 接口:程序世界的通用语言与交互基因
  • 据字典是什么?和数据库、数据仓库有什么关系?
  • 《中国棒垒球》奥运会金牌排名·棒球1号位
  • 2025 渗透工具:【中国蚁剑】连接一句话MUA文件 远控虚拟机靶机
  • Web 应用防火墙(WAF)工作原理、防护策略与部署模式深度剖析
  • Spring-创建第一个SpringBoot项目
  • Flutter中FutureBuilder和StreamBuilder
  • 解决Vue再浏览器的控制台中更新属性不生效
  • C++实现手写strstr函数
  • 什么是池化
  • Arduino Nano 33 BLE Sense Rev 2开发板使用指南之【环境搭建 / 点灯】
  • 数据库连接池(Druid、HikariCP)详解