Commant Calculer le Nombre de jour total et ouvrable en Laravel et JavaScripts?
To calculate the total number of working days between two dates in Laravel and JavaScript, you can use the Carbon library for handling dates and time in Laravel and JavaScript Date object for JavaScript. Here's a step-by-step process to achieve this:
- Install Carbon library in Laravel:
If you haven't installed Carbon library yet, you can install it using composer:
composer require carbon/carbon
- Import Carbon library in your Laravel controller or service:
use Carbon\Carbon;
- Define the start and end dates in Laravel:
$startDate = Carbon::parse('2023-03-01');
$endDate = Carbon::parse('2023-03-31');
- Calculate the total number of days between two dates in Laravel:
$totalDays = $startDate->diffInDays($endDate);
- Determine the number of working days between two dates in Laravel:
Assuming weekends (Saturday and Sunday) are non-working days, you can calculate the number of working days as follows:
$workingDays = 0;
for ($i = 0; $i < $totalDays; $i++) {
$date = $startDate->addDays($i);
if ($date->isWeekend()) {
continue;
}
$workingDays++;
}
$workingDaysTotal = $workingDays;
- Pass the total and working days to the view:
return view('yourview', ['totalDays' => $totalDays, 'workingDaysTotal' => $workingDaysTotal]);
- Calculate the total and working days in JavaScript:
const startDate = new Date('2023-03-01');
const endDate = new Date('2023-03-31');
const totalDays = Math.floor((endDate - startDate) / (1000 * 60 * 60 * 24)) + 1;
let workingDays = 0;
for (let i = 0; i < totalDays; i++) {
const currentDate = new Date(startDate.getTime());
currentDate.setDate(startDate.getDate() + i);
const dayOfWeek = currentDate.getDay();
if (dayOfWeek === 0 || dayOfWeek === 6) {
continue;
}
workingDays++;
}
console.log('Total days:', totalDays);
console.log('Working days:', workingDays);
Keep in mind that the JavaScript code snippet provided above calculates the working days based on the assumption that weekends are non-working days. You can modify the code to fit your specific requirements.