我确实有这个变量
$year = 2014;
$month = 02;
$cntWeek = 2;
如何获取2014年2月第二周的日期
我必须得到
2014-02-02
2014-02-03
2014-02-04
2014-02-05
2014-02-06
2014-02-07
2014-02-08
提前感谢。
答案 0 :(得分:0)
这是一个快速的功能,我把它放在一起似乎有效:
<?php
function get_nth_week_of_month($year, $month, $cntWeek) {
$begin = new DateTime( "$year-$month-01" );
$end = clone($begin);
$end->modify("+1 month");
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$dates = array();
$cur_week = 1;
foreach($daterange as $date) {
if ($date->format("N") == 7 && $date->format('d') != 1) {
$cur_week++;
}
if ($cur_week == $cntWeek) {
$dates[] = $date->format("Y-m-d");
}
}
return $dates;
}
print_r(get_nth_week_of_month(2014, 2, 2));
以上的输出是:
Array
(
[0] => 2014-02-02
[1] => 2014-02-03
[2] => 2014-02-04
[3] => 2014-02-05
[4] => 2014-02-06
[5] => 2014-02-07
[6] => 2014-02-08
)