<?php
//获取范围内的月份
function getMonthsInRange($startDate, $endDate) {
$start = new DateTime($startDate);
$end = new DateTime($endDate);
$months = array();
while ($start <= $end) {
$months[] = $start->format('Y-m');
$start->modify('first day of next month');
}
return $months;
}
//计算日期段内有多少天
function getDays($startDate, $endDate) {
$stimestamp = strtotime($startDate);
$etimestamp = strtotime($endDate);
return ($etimestamp-$stimestamp)/86400+1;
}
/**
*
* @param Date $startdate 开始日期
* @param Date $enddate 结束日期
* @return Array
*/
function getDateFromRange($startdate, $enddate) {
$stimestamp = strtotime($startdate);
$etimestamp = strtotime($enddate);
// 计算日期段内有多少天
$days = ($etimestamp-$stimestamp)/86400+1;
// 保存每天日期
$date = [];
for($i=0; $i<$days; $i++){
$date[] = date('Y-m-d', $stimestamp+(86400*$i));
}
return $date;
}
php获取一段时间中详细日期,月份,天数
于 2023-09-08 16:26:00 首次发布
本文介绍了PHP中的三个用于处理日期范围的函数:getMonthsInRange用于获取两个日期之间的月份列表,getDays计算日期段内的总天数,getDateFromRange生成指定日期范围内的完整日期数组。
摘要由CSDN通过智能技术生成