4
I’m using a script I found in this question Soen, and to count the working days disregarding Saturday and Sunday (can only do with Sunday tbm, changing $what_day > 5
for $what_day > 6
for example) it works right, but I wanted to include the official calendar of holidays in Brazil.
This is the script that is working (already taking the data from my form):
<?php
//get current month for example
$beginday = ($_POST ["Tdesl"]);
$lastday = ($_POST ["Tinsem3"]);
$nr_work_days = getWorkingDays($beginday, $lastday);
echo $nr_work_days;
function getWorkingDays($startDate, $endDate)
{
$begin = strtotime($startDate);
$end = strtotime($endDate);
if ($begin > $end) {
echo "startdate is in the future! <br />";
return 0;
} else {
$no_days = 0;
$weekends = 0;
while ($begin <= $end) {
$no_days++; // no of days in the given interval
$what_day = date("N", $begin);
if ($what_day > 5) { // 6 and 7 are weekend days
$weekends++;
};
$begin += 86400; // +1 day
};
$working_days = $no_days - $weekends;
return $working_days;
}
}
THE HTML:
<form method="post" id="Cform" name="Tform" action="diasuteis2.php">
<label for="Cinsem">Data inicial:</label>
<input type="date" name="Tinsem3" id="Cinsem" size="6">
<label for="Cdesl22">Data final:</label>
<input type="date" name="Tdesl" id="Cdesl22" size="6"><br><br>
<p align="center">
<input type="submit" id="enviar"></p>
</form>
An example of correct output for:
echo $beginday;
echo "<br><br>";
echo $lastday;
echo "<br><br>";
echo $nr_work_days;
IS:
2014-12-10
2014-12-15
4
(He considered the 10th, and excluded the 13th and 14th correctly)
Is it possible or feasible to include holidays in this code? If someone can make an example (of course you don’t need to include all holidays, just show how I can do it) I appreciate it now.
Basically you need to have a list, an array of holiday dates. I suggest that you avoid defining Saturday and Sunday as "non-working" days because that is not always the case. Restaurants, for example, sell more on the weekends and usually take off on weekdays when it has low movement. For these, Saturday and Sunday are working days. As for holidays, it also needs to assess regional and municipal holidays such as indefinite holidays. You need to focus more on the logic of holidays so you can play with the calendar. Of course, all this if you want to make the script reusable.
– Daniel Omine
I found an interesting package from a quick search. However, it is necessary to define holidays via array https://github.com/ministryofjustice/date-utils
– gmsantos