资源来自网络,非原创。
//获取两个日期区间所有日期,两个参数分别为两个日期 Y-m-d
public 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;
}
调用:getDateFromRange('2017-1-8', '2017-2-8');
$date = date('oW', strtotime('2017-1-8')); //获取日期所在哪年的第几周 $date = 201701
//获取 2017-1-8 这天属于 2017 的第 1 周。判断方式是从某一年的第一个 周一 开始计算新的一年的第一周
//根据第几周获取当周的开始日期与最后日期
function weekday($year,$weeknum){
$firstdayofyear=mktime(0,0,0,1,1,$year);
$firstweekday=date('N',$firstdayofyear);
$firstweenum=date('W',$firstdayofyear);
if($firstweenum==1){
$day=(1-($firstweekday-1))+7*($weeknum-1);
$startdate=date('Y-m-d',mktime(0,0,0,1,$day,$year));
$enddate=date('Y-m-d',mktime(0,0,0,1,$day+6,$year));
}else{
$day=(9-$firstweekday)+7*($weeknum-1);
$startdate=date('Y-m-d',mktime(0,0,0,1,$day,$year));
$enddate=date('Y-m-d',mktime(0,0,0,1,$day+6,$year));
}
return array($startdate,$enddate);
}
调用:weekday('2017', '01');
$month = date('Y-m', strtotime('2017-1-8')); //获取月份 $month = 2017-01
//获取某个月的所有日期
function getMonthDays($month = "this month", $format = "Y-m-d", $dateTimeZone = false)
{
if(!$dateTimeZone) $dateTimeZone = new DateTimeZone("Asia/Shanghai");
$start = new DateTime("first day of $month", $dateTimeZone);
$end = new DateTime("last day of $month", $dateTimeZone);
$days = array();
for($time = $start; $time <= $end; $time = $time->modify("+1 day")) {
$days[] = $time->format($format);
}
return $days;
}
调用: getMonthDays('2017-01');
参照链接:
https://www.cnblogs.com/sanqiyi/p/8267019.html
https://www.cnblogs.com/leilei-1/p/8028183.html
https://segmentfault.com/q/1010000002415326