I’m trying to make a percentage chart that display how many employee who is absent who is not (means not recorded to absent_table), on current day on that month (example : today, is 27-07-2023, which means we are on 27 order day number). my algorythm about this is that, first we need to know the total absent best case until current day. so if I have 5 employee, then 5*27 is best case of total absent (it means all employee is absent and recorded on absent_table, this will be the divider. the rest I can handle it). my problem is that how to determine what order number day till current day, if weekend being passed. for example if today is 27 july 2023, then the ordered day number must 19, correct? (weekend for me means saturday and sunday)
to get current day is easy, i just do :
$current_day = date("d");
but i have to subtract it with total of weekend until that week. for me it’s 4th week now, so :4 * 2 = 8 total week
so the formula become like this :
$ordered_day = (int)date("d") - $total_week; //I need to find this
I hope you guys understand. thank you.
EDIT :
Here’s the final code look like, thanks to @Mpmp :
$total_weekdays = getWeekdaysBetweenDates(date("Y-m-01"), date("Y-m-d"));
$total_absent_best_case = $total_weekdays * $total_employee <-- of course, you fetch it from database
>Solution :
I’m not sure I understand but
function getWeekdaysBetweenDates($date1, $date2) {
$startDate = DateTime::createFromFormat('d-m-Y', $date1);
$endDate = DateTime::createFromFormat('d-m-Y', $date2);
$period = new DatePeriod(
$startDate,
new DateInterval('P1D'),
$endDate->modify('+1 day')
);
$weekdays = 0;
foreach ($period as $day) {
if ($day->format('N') < 6) {
$weekdays++;
}
}
return $weekdays;
}
$date1 = '27-07-2023';
$date2 = '27-08-2023';
$weekdaysBetweenDates = getWeekdaysBetweenDates($date1, $date2);
echo $weekdaysBetweenDates; // 22