我想在Laravel中使用碳纤维在一个月内获得工作日期,例如2020年3月,我想获得2020年3月起的所有日期,而没有周末(星期六和星期日)的日期,这样,
2020-03-02
2020-03-03
2020-03-04
2020-03-05
2020-03-06
2020-03-09
2020-03-10
2020-03-11
2020-03-12
2020-03-13
2020-03-16
2020-03-17
2020-03-18
2020-03-19
2020-03-20
2020-03-23
2020-03-24
2020-03-25
2020-03-26
2020-03-27
2020-03-30
2020-03-31
可能吗?
您可以使用PHP如下:
$workdays = array();
$type = CAL_GREGORIAN;
$month = date('n'); // Month ID, 1 through to 12.
$year = date('Y'); // Year in 4 digit 2009 format.
$day_count = cal_days_in_month($type, $month, $year); // Get the amount of days
//loop through all days
for ($i = 1; $i <= $day_count; $i++) {
$date = $year.'/'.$month.'/'.$i; //format date
$get_name = date('l', strtotime($date)); //get week day
$day_name = substr($get_name, 0, 3); // Trim day name to 3 chars
//if not a weekend add day to array
if($day_name != 'Sun' && $day_name != 'Sat'){
$workdays[] = date( "Y-m-d", strtotime($i."-".$month."-".$year));
}
}
print_r($workdays);
输出:
Array
(
[0] => 2020-03-02
[1] => 2020-03-03
[2] => 2020-03-04
[3] => 2020-03-05
[4] => 2020-03-06
[5] => 2020-03-09
[6] => 2020-03-10
[7] => 2020-03-11
[8] => 2020-03-12
[9] => 2020-03-13
[10] => 2020-03-16
[11] => 2020-03-17
[12] => 2020-03-18
[13] => 2020-03-19
[14] => 2020-03-20
[15] => 2020-03-23
[16] => 2020-03-24
[17] => 2020-03-25
[18] => 2020-03-26
[19] => 2020-03-27
[20] => 2020-03-30
[21] => 2020-03-31
)
您可以使用filter()
方法将Carbon的isWeekday()
应用于日期期间:
$period = CarbonPeriod::between('2020-03-01', '2020-03-31')->filter('isWeekday');
foreach ($period as $date) {
$days[] = $date->format('Y-m-d');
}
echo implode('<br>', $days);
这将打印出来
2020-03-02
2020-03-03
2020-03-04
2020-03-05
2020-03-06
2020-03-09
2020-03-10
2020-03-11
2020-03-12
2020-03-13
2020-03-16
2020-03-17
2020-03-18
2020-03-19
2020-03-20
2020-03-23
2020-03-24
2020-03-25
2020-03-26
2020-03-27
2020-03-30
2020-03-31
希望这个有帮助...
$month = '2020-03';
$start = Carbon\Carbon::parse($month)->startOfMonth();
$end = Carbon\Carbon::parse($month)->endOfMonth();
$dates = [];
while ($start->lte($end)) {
$carbon = Carbon\Carbon::parse($start);
if ($carbon->isWeekend() !=true) {
$dates[] = $start->copy()->format('Y-m-d');
}
$start->addDay();
}
foreach ($dates as $key => $dateval) {
echo "<br>".$dateval;
}