Laravel 7: 查找两个日期之间有条件的天数和日期
Laravel 7: Find days and dates between two dates with condition
用户提交请假申请时,需要select开始和结束日期。但在保存之前需要检查两个日期之间是否有任何假期或每周假期。如果假期或每周假期匹配,则将从总天数中扣除假期总数。
每周假期:
id
day
working_status
1
Fri
1
假日Table:(模型名称:假日)
id
date
publication_status
1
2022-05-26
1
离开Table:
id
start_date
end_date
total_days
1
2022-05-25
2022-05-28
2
控制器:
$leave = new User;
$leave->start_date = $request->start_date;
$leave->end_date = $request->end_date;
//get and convert day name for weekly holiday compare like Fri, Sat etc.
$start_day = date("D", strtotime($request->start_date));
$end_day = date("D", strtotime($request->end_date));
// get and convert date for monthly holiday compare
$start = strtotime($request->start_date);
$end = strtotime($request->end_date);
$diff = $end - $start;
$diff_in_days = floor($diff/(60*60*24)) + 1;
// Suppose Fri is holiday now we have to count how many Fri day in between start and end date.
here need help
$weekly_holidays = WorkingDay::where('working_status', 1)
->get(['day'])->count();
// we have to count how many Monthly holiday in between start and end date. here need help
$monthly_holidays= Holiday::where('publication_status', 1)->get(['date'])->count();
$total_days = $diff_in_days - ($weekly_holidays + $monthly_holidays);
if($request->halfday == 1){
$leave->total_days = 0.5;
}
else{
$leave->total_days = $total_days;
}
示例:
WorkingDay = day = Fri
Holiday = date = 2022-05-26
start_date = 2022-05-25
end_date = 2022-05-28
total_days = 2
// from 25 to 28, the total days are 4, but 26 is a holiday, and 27 is Friday. Holidays can be multiple dates. If there is multiple holidays between the start and end date, it will calculate according to this.
实际上更容易。你不需要把它复杂化。 Carbon
来拯救这里。
$startDate = Carbon::parse('start date here');
$endDate = Carbon::parse('date string here');
$weekdays = $startDate->diffInWeekdays($endDate);
这将为您提供工作日的天差,不包括任何周末。
现在无论您使用哪个数据库,您都可以使用一个函数来获取星期几 0 stands for Sunday
和 6 for Saturday
。
对于Postgres,我举个例子
weekdays_holiday_count = select count(*) from holidays where date_part('dow', date) not in (0, 6) # 0 for Sunday and 6 for Saturday
此查询计算工作日的假期。我在这里给你原始查询。不过,这里的重点是在函数的魔力下。
现在你可以轻松地做数学了
$weekdays - $weekdays_holiday_count
确保在顶部导入 Carbon\Carbon
。
用户提交请假申请时,需要select开始和结束日期。但在保存之前需要检查两个日期之间是否有任何假期或每周假期。如果假期或每周假期匹配,则将从总天数中扣除假期总数。
每周假期:
id | day | working_status |
---|---|---|
1 | Fri | 1 |
假日Table:(模型名称:假日)
id | date | publication_status |
---|---|---|
1 | 2022-05-26 | 1 |
离开Table:
id | start_date | end_date | total_days |
---|---|---|---|
1 | 2022-05-25 | 2022-05-28 | 2 |
控制器:
$leave = new User;
$leave->start_date = $request->start_date;
$leave->end_date = $request->end_date;
//get and convert day name for weekly holiday compare like Fri, Sat etc.
$start_day = date("D", strtotime($request->start_date));
$end_day = date("D", strtotime($request->end_date));
// get and convert date for monthly holiday compare
$start = strtotime($request->start_date);
$end = strtotime($request->end_date);
$diff = $end - $start;
$diff_in_days = floor($diff/(60*60*24)) + 1;
// Suppose Fri is holiday now we have to count how many Fri day in between start and end date.
here need help
$weekly_holidays = WorkingDay::where('working_status', 1)
->get(['day'])->count();
// we have to count how many Monthly holiday in between start and end date. here need help
$monthly_holidays= Holiday::where('publication_status', 1)->get(['date'])->count();
$total_days = $diff_in_days - ($weekly_holidays + $monthly_holidays);
if($request->halfday == 1){
$leave->total_days = 0.5;
}
else{
$leave->total_days = $total_days;
}
示例:
WorkingDay = day = Fri
Holiday = date = 2022-05-26
start_date = 2022-05-25
end_date = 2022-05-28
total_days = 2
// from 25 to 28, the total days are 4, but 26 is a holiday, and 27 is Friday. Holidays can be multiple dates. If there is multiple holidays between the start and end date, it will calculate according to this.
实际上更容易。你不需要把它复杂化。 Carbon
来拯救这里。
$startDate = Carbon::parse('start date here');
$endDate = Carbon::parse('date string here');
$weekdays = $startDate->diffInWeekdays($endDate);
这将为您提供工作日的天差,不包括任何周末。
现在无论您使用哪个数据库,您都可以使用一个函数来获取星期几 0 stands for Sunday
和 6 for Saturday
。
对于Postgres,我举个例子
weekdays_holiday_count = select count(*) from holidays where date_part('dow', date) not in (0, 6) # 0 for Sunday and 6 for Saturday
此查询计算工作日的假期。我在这里给你原始查询。不过,这里的重点是在函数的魔力下。
现在你可以轻松地做数学了
$weekdays - $weekdays_holiday_count
确保在顶部导入 Carbon\Carbon
。