-3

I have a date like this

    $start = strtotime('2010-01-01'); $end = strtotime('2010-01-25');

My question:

How can I calculate or count weekend from $start & $end date range..??

Rhys
  • 4,511
  • 2
  • 23
  • 32
b4dQuetions
  • 1,549
  • 6
  • 18
  • 28

3 Answers3

1

A more modern approach is using php's DateTime class. Below, you get an array with week numbers as keys. I added the counts of weeks and weekend days.

<?php

$begin = new DateTime('2010-01-01');
$end = new DateTime('2010-01-25');

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);
$weekends = [];

foreach($daterange as $date) {
    if (in_array($date->format('N'), [6,7])) {
        $weekends[$date->format('W')][] = $date->format('Y-m-d');
    }
}

print_r($weekends);

echo 'Number of weeks: ' . count($weekends);

echo 'Number of weekend days: ' . (count($weekends, COUNT_RECURSIVE) - count($weekends));

Note: if you're using PHP 5.3, use array() instead of block arrays [].

schellingerht
  • 5,726
  • 2
  • 28
  • 56
  • hy Schellingerht thank you for your answer but I have quetion about your code what the meaning **new DateInterval('P1D');** – b4dQuetions Jan 26 '16 at 08:53
  • DateInterval are the steps in the range. P1D means per step 1 day. Because you don't know before what's the day number of the starting day. In the foreach loop, every day from start to end passes. Then, I check if the day number is 6 (Saturday) or 7 (Sunday). – schellingerht Jan 26 '16 at 08:55
  • Hi @bd4dQuetions. Nice! Please mark the right answer for people with the same problem ;-) – schellingerht Jan 26 '16 at 09:06
0

May be this code snippet will help:

<?php
    //get current month for example
    $beginday = date("Y-m-01");
    $lastday  = date("Y-m-t");

    $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;
        }
    }

Another solution can be: (Get date range between two dates excluding weekends)

Community
  • 1
  • 1
Iresha Rubasinghe
  • 913
  • 1
  • 10
  • 27
0

This might help maybe:

$start = strtotime('2010-01-01'); 
$end = strtotime('2010-01-25');

$differ = $end-$start;

$min = $differ/60;
$hrs = $min/60;
$days = $hrs/24;
$weeks = $days/7;


if(is_int($weeks))
$weeks++;

echo '<pre>';
print_r(ceil($weeks));
echo '</pre>';
pritesh
  • 2,162
  • 18
  • 24