-1

I'm trying to get the count for weekdays and weekends between two dates but for weekends I'm getting 0 value .

Here is the code :

<?php

function daysCount($startDate, $endDate)
{
    $weekdayCount = $weekendCount  =0;
    $startTimestamp = strtotime($startDate);
    $endTimestamp = strtotime($endDate);

    for ($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24))
    {
        if (date("N", $i) <= 5)
        {

            $weekdayCount = $weekdayCount + 1;

        }
        if (date("N", $i) == 6 && $i%7 == 0 )
        {
            $weekendCount = $weekendCount + 1;
        }
    }
    return array('weekdayCount' => $weekdayCount, 'weekendCount' => $weekendCount);
}

$startDate = "2017-07-03";
$endDate = "2017-07-10";
$days = daysCount($startDate, $endDate);
print_r($days);

?>

And this is the demo link demo updated

Can somebody help me to get the weekendCount ?

06011991
  • 797
  • 2
  • 13
  • 39
  • Thats not the same code as your demo, which code are we correcting? – Flosculus Jul 10 '17 at 11:08
  • also by weekends, does this mean as a group? e.g. a Saturday and Sunday would count as 1 weekend? Or is it, Saturday and Sunday are both separate weekend days? – treyBake Jul 10 '17 at 11:09
  • @Flosculus : The code which i have given we are correcting – 06011991 Jul 10 '17 at 11:11
  • @ThisGuyHasTwoThumbs : both are separate like lets says Saturday ans Sunday should count as 2 days. – 06011991 Jul 10 '17 at 11:12
  • Possible duplicate of [Calculate difference between two dates (number of days)?](https://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days) – treyBake Jul 10 '17 at 11:14

1 Answers1

2

Maybe this way, base on the demo link:

function number_of_working_days($startDate, $endDate)
{
    $workingDays = 0;
    $startTimestamp = strtotime($startDate);
    $endTimestamp = strtotime($endDate);
    for ($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24)) {
        if (date("N", $i) <= 5) $workingDays = $workingDays + 1;
    }
    return $workingDays;
}

function number_of_weekend_days($startDate, $endDate)
{
    $weekendDays = 0;
    $startTimestamp = strtotime($startDate);
    $endTimestamp = strtotime($endDate);
    for ($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24)) {
        if (date("N", $i) > 5) $weekendDays = $weekendDays + 1;
    }
    return $weekendDays;
}

$startDate = "2016-11-01";
$endDate = "2016-11-30";
$workingDays = number_of_working_days($startDate, $endDate);
$weekendDays = number_of_weekend_days($startDate, $endDate);
echo "Total number of working days between $startDate and $endDate is: " . $workingDays . " day(s).";
echo "Total number of weekend days between $startDate and $endDate is: " . $weekendDays . " day(s).";
Phoenixy
  • 104
  • 1
  • 14