PHP获取本周所有日期或者最近七天所有日期
码农编程进阶笔记
共 713字,需浏览 2分钟
·
2020-08-05 23:15
本篇文章主要为大家介绍下php如何获取本周的所有日期,或者最近七天的所有日期。希望可以帮助到有需要的朋友
获取本周所有日期:
/**
* 获取本周所有日期
*/
function get_week($time = '', $format='Y-m-d'){
$time = $time != '' ? $time : time();
//获取当前周几
$week = date('w', $time);
$date = [];
for ($i=1; $i<=7; $i++){
$date[$i] = date($format ,strtotime( '+' . $i-$week .' days', $time));
}
return $date;
}
执行结果:
print_r(get_week());
Array
(
[1] => 2018-06-18
[2] => 2018-06-19
[3] => 2018-06-20
[4] => 2018-06-21
[5] => 2018-06-22
[6] => 2018-06-23
[7] => 2018-06-24
)
获取最近七天日期:
/**
* 获取最近七天所有日期
*/
function get_weeks($time = '', $format='Y-m-d'){
$time = $time != '' ? $time : time();
//组合数据
$date = [];
for ($i=1; $i<=7; $i++){
$date[$i] = date($format ,strtotime( '+' . $i-7 .' days', $time));
}
return $date;
}
执行结果:
print_r(get_weeks());
Array
(
[1] => 2018-06-13
[2] => 2018-06-14
[3] => 2018-06-15
[4] => 2018-06-16
[5] => 2018-06-17
[6] => 2018-06-18
[7] => 2018-06-19
)
评论