$date = date('Y-m-d') // you can put any date you want
$nbDay = date('N', strtotime($date));
$monday = new DateTime($date);
$sunday = new DateTime($date);
$monday->modify('-'.($nbDay-1).' days');
$sunday->modify('+'.(7-$nbDay).' days');
我发现这个解决方案很有帮助。如果不是星期一就减去前一个星期一。我使用 $lower _ date 作为从查询中提取的日期,然后需要将该日期与前一个星期一进行协调。
//set this up to go backwards until you find monday
while(date('D',strtotime($lower_date))!='Mon'){
$lower_date = date('Y-m-d', strtotime($lower_date . ' - 1 day')); //increase the upper spec
}
$dateString = '02-21-2015'; // example in format MM-dd-yyyy
$date = Carbon::createFromFormat('m-d-Y', $dateString);
$date->hour(0)->minute(0)->second(0)->startOfWeek();
$dateString = $date->Format('m-d-Y'); // and you have got a string value "02-16-2015"