Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中获取每月的开始/结束星期一_Php_Date_Datetime - Fatal编程技术网

如何在php中获取每月的开始/结束星期一

如何在php中获取每月的开始/结束星期一,php,date,datetime,Php,Date,Datetime,我还需要包括上个月的最后一个星期一和下个月的第一个星期一 范例 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 ' . $ye

我还需要包括上个月的最后一个星期一和下个月的第一个星期一

范例

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;
}
函数getAllDaysInAMonth($year、$month、$daysError=3){ $dateString='first'.$day.'of'.$year'.-'.$month; 如果(!strotime($dateString)){ 抛出新的\异常(“.”$dateString“.”不是有效的strotTime); } $startDay=new\DateTime($dateString); 如果($startDay->format('j')>$daysError){ $startDay->modify('-7天'); } $days=array(); 而($startDay->format('Y-m')修改('+7天'); } 返回$days; } 试试这个:

$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');
$lastMonday=新日期时间(“上个月的最后一个星期一”);
$nextMonday=新的日期时间(“下个月的第一个星期一”);
回显“上周一:”.$LASTMONIDAY->格式('Y-m-d')。
; echo“第一个星期一:”.$nextMonday->格式('Y-m-d');
好的,解决了

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;
}
函数getAllDaysInAMonth($year、$month、$daysError=3){ $dateString='first'.$day.'of'.$year'.-'.$month; 如果(!strotime($dateString)){ 抛出新的\异常(“.”$dateString“.”不是有效的strotTime); } $startDay=new\DateTime($dateString); 如果($startDay->format('j')>$daysError){ $startDay->modify('-7天'); } $days=array(); $lastMonday=新日期时间(“上个月的最后一个星期一”); $nextMonday=新的日期时间(“下个月的第一个星期一”); $days[]=克隆($lastMonday); 而($startDay->format('Y-m')修改('+7天'); } $days[]=克隆($nextMonday); 返回$days; }
请添加您目前拥有的代码