Database 如何使用碳元素在一个月内确定工作日期?
我想在一个月内用Laravel的碳来确定一个工作日期, 例如,在2020年3月,我希望获得2020年3月的所有日期,不包括周末(周六和周日)日期, 像这样,Database 如何使用碳元素在一个月内确定工作日期?,database,laravel,php-carbon,Database,Laravel,Php Carbon,我想在一个月内用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 2
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);
您可以使用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);
您可以使用
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);
您可以使用
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);
希望这有帮助
$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;
}
$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);
如果($carbon->isWeekend()!=true){
$dates[]=$start->copy()->格式('Y-m-d');
}
$start->addDay();
}
foreach($key=>$dateval的日期){
echo“
”$dateval;
}
希望这有帮助
$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;
}
$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);
如果($carbon->isWeekend()!=true){
$dates[]=$start->copy()->格式('Y-m-d');
}
$start->addDay();
}
foreach($key=>$dateval的日期){
echo“
”$dateval;
}