我还需要包括上个月的最后一个星期一和下个月的第一个星期一。
实施例
2016-01-25
2016-02-01
2016-02-08
2016-02-15
2016-02-22
2016-02-29
2016-03-07
到目前为止我有这个代码:
function getAllDaysInAMonth($year, $month, $day = 'Monday', $daysError = 3) {
$dateString = 'first ' . $day . ' of ' . $year . '-' . $month;
if (!strtotime($dateString)) {
throw new \Exception('"' . $dateString . '" is not a valid strtotime');
}
$startDay = new \DateTime($dateString);
if ($startDay->format('j') > $daysError) {
$startDay->modify('- 7 days');
}
$days = array();
while ($startDay->format('Y-m') <= $year . '-' . str_pad($month, 2, 0, STR_PAD_LEFT)) {
$days[] = clone($startDay);
$startDay->modify('+ 7 days');
}
return $days;
}
答案 0 :(得分:1)
好的,已经解决了!
function getAllDaysInAMonth($year, $month, $day = 'Monday', $daysError = 3) {
$dateString = 'first ' . $day . ' of ' . $year . '-' . $month;
if (!strtotime($dateString)) {
throw new \Exception('"' . $dateString . '" is not a valid strtotime');
}
$startDay = new \DateTime($dateString);
if ($startDay->format('j') > $daysError) {
$startDay->modify('- 7 days');
}
$days = array();
$lastMonday = new DateTime("last Monday of last month");
$nextMonday = new DateTime("first Monday of next month");
$days[] = clone($lastMonday);
while ($startDay->format('Y-m') <= $year . '-' . str_pad($month, 2, 0, STR_PAD_LEFT)) {
$days[] = clone($startDay);
$startDay->modify('+ 7 days');
}
$days[] = clone($nextMonday);
return $days;
}
答案 1 :(得分:0)
试试这个:
$lastMonday = new DateTime("last Monday of last month");
$nextMonday = new DateTime("first Monday of next month");
echo 'Last Monday : '.$lastMonday->format('Y-m-d').'<br />';
echo 'First Monday : '.$nextMonday->format('Y-m-d');