I'm working on a leave/holiday management system.
As well as booking full days leave/holiday they can book half days (morning or afternoon off).
I need to calculate the total amount of days during a requested period. 3 and a half days would be presented as 3.5
To make calculations easier for half days, the maths is based on a day starting at 00:00 and ending at 23:59:59.999999
I can do this with the following but it doesn't exclude weekends which are counted as part of a users leave quota.
if($validated['leave_start_day_part'] == 'AM'){
//Leave starts at the beginning of the day
$startDate = Carbon::createFromDate($validated['leave_start_date'])->startOfDay();
}else{
//Leave starts midday (half day)
$startDate = Carbon::createFromDate($validated['leave_start_date'])->midDay();
}
if($validated['leave_end_day_part'] == 'AM'){
//Leave ends at midday (half day)
$endDate = Carbon::createFromDate($validated['leave_end_date'])->midDay();
}else{
//Leave ends at the end of the day
$endDate = Carbon::createFromDate($validated['leave_end_date'])->endOfDay();
}
$dayCount = $startDate->copy()->diffInDays($endDate);
diffInDays returns a float which is perfect but also includes weekend days.
However if I switch that to the following it returns a whole integer (3.5 is returned as 3)
$dayCount = $startDate->copy()->diffInWeekdays($endDate);
I've considered using diffInDaysFiltered but that also only returns an integer. The decimal is crucial to calculate the total used leave. Is there another method of Carbon that can achieve this and output float or is it time to consider another route?
I'm working on a leave/holiday management system.
As well as booking full days leave/holiday they can book half days (morning or afternoon off).
I need to calculate the total amount of days during a requested period. 3 and a half days would be presented as 3.5
To make calculations easier for half days, the maths is based on a day starting at 00:00 and ending at 23:59:59.999999
I can do this with the following but it doesn't exclude weekends which are counted as part of a users leave quota.
if($validated['leave_start_day_part'] == 'AM'){
//Leave starts at the beginning of the day
$startDate = Carbon::createFromDate($validated['leave_start_date'])->startOfDay();
}else{
//Leave starts midday (half day)
$startDate = Carbon::createFromDate($validated['leave_start_date'])->midDay();
}
if($validated['leave_end_day_part'] == 'AM'){
//Leave ends at midday (half day)
$endDate = Carbon::createFromDate($validated['leave_end_date'])->midDay();
}else{
//Leave ends at the end of the day
$endDate = Carbon::createFromDate($validated['leave_end_date'])->endOfDay();
}
$dayCount = $startDate->copy()->diffInDays($endDate);
diffInDays returns a float which is perfect but also includes weekend days.
However if I switch that to the following it returns a whole integer (3.5 is returned as 3)
$dayCount = $startDate->copy()->diffInWeekdays($endDate);
I've considered using diffInDaysFiltered but that also only returns an integer. The decimal is crucial to calculate the total used leave. Is there another method of Carbon that can achieve this and output float or is it time to consider another route?
Share Improve this question edited Mar 11 at 7:32 Olivier 18.4k1 gold badge11 silver badges31 bronze badges asked Mar 10 at 12:58 PeteBPeteB 13311 bronze badges 02 Answers
Reset to default 2There isn't any other function that will return a float. However, diffInWeekendDays
is an option. Take your current number of days, and subtract the result of that function.
$weekend_days = $startDate->copy()->diffInWeekendDays($endDate);
$dayCount -= $weekend_days;
The solution you currently have would assume the day's half is at 12:00.
With that assumption in mind you could also take the diff in hours and apply modulo 24 over them (120%24 -> 12).
If the value resulted is exactly 12 than you can consider it half day, but by using this you will only allow splitting the day in to halves.
I'd recommend using a different mechanism for the hours off.
Calculate the full days + compute the working hours that you want to take off afterwards.
Assuming working day is 08-16 (to accomodate the 12:00 half day)
Having the difference as float won't help as if an employee takes time off from Thursday 16:00 until Monday 10:00 the difference would be 1.7 when in fact a full working day and 2 hours were actually taken off, which would compute to 1.25.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744845696a4596821.html
评论列表(0条)