Counting of week-specific days between two dates

Asked

Viewed 806 times

1

I’m trying to figure out how many week-specific days there are between two time periods.

Like I said in this question, I am using this script, adapted with the help of this answer, to achieve working days by removing Saturdays, Sundays and national holidays:

$beginday = isset($_POST["Tinsem3"]) ? $_POST["Tinsem3"] : false;
$lastday  = isset($_POST["Tdesl"]) ? $_POST["Tdesl"] : false;

$nr_work_days = getWorkingDays($beginday, $lastday);

function getWorkingDays($startDate, $endDate) {
    $begin = strtotime($startDate);
    $end   = strtotime($endDate);
    if ($begin > $end) {
        echo "startdate is in the future! <br />";
        return 0;
    }
    else {
        $holidays = array('01/01', '03/04', '21/04', '01/05', '07/09', '12/10', '02/11', '15/11', '25/12');
        $weekends = 0;
        $no_days = 0;
        $holidayCount = 0;
        while ($begin <= $end) {
            $no_days++; // no of days in the given interval
            if (in_array(date("d/m", $begin), $holidays)) {
                $holidayCount++;
            }
            $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 - $holidayCount;

        return $working_days;
    }
}

But I would now like to take how many specific days of the week there are between these two periods. I have very basic knowledge of PHP, so a good explanation would also be welcome.

I know that to get only the specific days is just to take the difference and count how many days of the week there are between the dates (that does not need this whole script). But there is a specific function for this?

Something like:

function getWednesDays($startDate, $endDate) {
$begin = strtotime($startDate);
$end   = strtotime($endDate);
$totaldequartasfeiras = XXX 

I added the script because I also need national holidays not to be included, so it would be nice if you could "enjoy" this part.

1 answer

1

Before the while creates the following variable:

$array_semana = array();

And inside the while soon after: $begin += 86400; // +1 day, insert:

$array_semana[$what_day]++;

So at the end of the while you will have the amount of days of each day of the week within the $array_semana

  • So I did it but it didn’t work. A var_dump of array_semana returns NULL. Do I need to include the names (or predetermined abbreviations) of the days of the week within array()? How it will return the specific days of the week just with $array_semana[$what_day]++;? I’m still a beginner in PHP, and I appreciate the help and explanations.

  • 1

    within the $array_week, it will store the amount of days of the week in each input of the array, for example the input 1 of the array_week corresponds to the amount of Mondays, the 2nd Tuesday, 3rd Wednesday and so on. But a question $array_semana[$what_day]++ Voce put inside while and var_dump after while is over?

  • So by putting the var_dump inside the block while (well after $array_semana[$what_day]++;), before return, then it returns array(1) { [3]=> int(1) } array(2) { [3]=> int(1) [4]=> int(1) } etc... but what I want is to take each day of the week, count how many were between periods and play these values within variables, as for example: array_segunda, array_terça, or within vectors, array_semana[1] how many seconds, array_semana[2] be how many Tuesdays, so I can use these variables (with the amount of days) in calculations etc... Thanks for the force.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.