Php calculates how many weekends there are in a month.

A solution found is as follows:

function get_weekend_days($start_date, $end_date, $weekend_days=1) {

    $data = array();

    $start_reduce = $end_add = 0;
    $start_N      = date("N",strtotime($start_date));
    $start_reduce = ($start_N == 7) ? 1 : 0;

    $end_N = date("N",strtotime($end_date));

    // 
    $weekend_days = intval($weekend_days);
    switch ($weekend_days)
    {
        case 2:
            in_array($end_N,array(6,7)) && $end_add = ($end_N == 7) ? 2 : 1;
            break;
        case 1:
        default:
            $end_add = ($end_N == 7) ? 1 : 0;
            break;
    }

    $days = ((strtotime($end_date) - strtotime($start_date))/86400) + 1;
    $data["total_days"] = $days;
    $data["total_relax"] = floor(($days + $start_N - 1 - $end_N) / 7) * $weekend_days - $start_reduce + $end_add;

    return $data;
}

Why is this $data ["total_relax"] calculated this way? This method doesn"t seem to work very well. For example, today is Sunday, but the returned $data ["total_relax"] is 0

.
Php
Feb.28,2021

try this modified

<?php

function get_weekend_days($start_date, $end_date, $weekend_days=1) {

$data = array();

$start_reduce = $end_add = 0;
$start_N      = date('N',strtotime($start_date));


$end_N = date('N',strtotime($end_date));

// 
$weekend_days = intval($weekend_days);
switch ($weekend_days)
{
    case 2:
        in_array($end_N,array(6,7)) && $end_add = ($end_N == 7) ? 2 : 1;
        $start_reduce = ($start_N == 7) ? 1 : 0;
        break;
    case 1:
    default:
        $end_add = ($end_N == 7) ? 1 : 0;
        break;
}

$days = ((strtotime($end_date) - strtotime($start_date))/86400) + 1;
$data['total_days'] = $days;
$data['total_relax'] = floor(($days + $start_N - 1 - $end_N) / 7) * $weekend_days - $start_reduce + $end_add;

return $data;
}

echo "Two days per week\n";
var_dump( get_weekend_days("2018-03-31","2018-04-08",2));
var_dump( get_weekend_days("2018-04-01","2018-04-08",2));
var_dump( get_weekend_days("2018-04-02","2018-04-08",2));
var_dump( get_weekend_days("2018-04-07","2018-04-08",2));
var_dump( get_weekend_days("2018-04-08","2018-04-08",2));
echo "One day per week\n";
var_dump( get_weekend_days("2018-03-31","2018-04-08",1));
var_dump( get_weekend_days("2018-04-01","2018-04-08",1));
var_dump( get_weekend_days("2018-04-02","2018-04-08",1));
var_dump( get_weekend_days("2018-04-07","2018-04-08",1));
var_dump( get_weekend_days("2018-04-08","2018-04-08",1));
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b378a2-2c083.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b378a2-2c083.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?