Get number of weekdays in a given month
Asked Answered
A

13

9

I want to calculate the number of weekdays days in a give month and year. Weekdays means monday to friday. How do i do it ?

Amu answered 6/12, 2011 at 7:19 Comment(0)
L
2

Some basic code:

$month = 12;
$weekdays = array();
$d = 1;

do {
    $mk = mktime(0, 0, 0, $month, $d, date("Y"));
    @$weekdays[date("w", $mk)]++;
    $d++;
} while (date("m", $mk) == $month);

print_r($weekdays);

Remove the @ if your PHP error warning doesn't show notices.

Lacerated answered 6/12, 2011 at 11:27 Comment(1)
-1 because I just hate error suppression with @ AND this code doesn't work as it should, it always returns one extra date in the next month.Meave
F
30

You don't need to count every day in the month. You already know the first 28 days contain 20 weekdays no matter what. All you have to do is determine the last few days. Change the start value to 29. Then add 20 weekdays to your return value.

function get_weekdays($m,$y) {
$lastday = date("t",mktime(0,0,0,$m,1,$y));
$weekdays=0;
for($d=29;$d<=$lastday;$d++) {
    $wd = date("w",mktime(0,0,0,$m,$d,$y));
    if($wd > 0 && $wd < 6) $weekdays++;
    }
return $weekdays+20;
}
Frigorific answered 15/10, 2012 at 20:59 Comment(1)
I love this solution for its logical analysis of only doing what is required. The true nature of efficient programming.Compiler
L
2

Some basic code:

$month = 12;
$weekdays = array();
$d = 1;

do {
    $mk = mktime(0, 0, 0, $month, $d, date("Y"));
    @$weekdays[date("w", $mk)]++;
    $d++;
} while (date("m", $mk) == $month);

print_r($weekdays);

Remove the @ if your PHP error warning doesn't show notices.

Lacerated answered 6/12, 2011 at 11:27 Comment(1)
-1 because I just hate error suppression with @ AND this code doesn't work as it should, it always returns one extra date in the next month.Meave
I
2

try this one

function getWeekdays($m, $y = NULL){
    $arrDtext = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');

    if(is_null($y) || (!is_null($y) && $y == ''))
        $y = date('Y');

    $d = 1;
    $timestamp = mktime(0,0,0,$m,$d,$y);
    $lastDate = date('t', $timestamp);
    $workingDays = 0;
    for($i=$d; $i<=$lastDate; $i++){
        if(in_array(date('D', mktime(0,0,0,$m,$i,$y)), $arrDtext)){
            $workingDays++;
        }
    }
    return $workingDays;
}
Italic answered 6/12, 2011 at 16:37 Comment(0)
B
1

This is the simplest code I could come up with. You really would need to create an array or a database table to hold the holidays to get a true, "Working Days" count, but that wasn't what was asked, so here you go, hope this helps someone.

function get_weekdays($m,$y) {
$lastday = date("t",mktime(0,0,0,$m,1,$y));
$weekdays=0;
for($d=1;$d<=$lastday;$d++) {
    $wd = date("w",mktime(0,0,0,$m,$d,$y));
    if($wd > 0 && $wd < 6) $weekdays++;
    }
return $weekdays;
}
Bronwen answered 8/2, 2012 at 15:54 Comment(0)
V
1

Get the number of working days without holidays between two dates :

Use example:

echo number_of_working_days('2013-12-23', '2013-12-29');

Output:

3

Link to the function

Venom answered 21/9, 2013 at 10:4 Comment(0)
L
1

DateObject method:

function getWorkingDays(DateTime $date) {
    $month = clone $date;
    $month->modify('last day of this month');
    $workingDays = 0;
    for ($i = $month->format('t'); $i > 28; --$i) {
        if ($month->format('N') < 6) {
            ++$workingDays;
        }
        $month->modify('-1 day');
    }

    return 20 + $workingDays;
}
Lachance answered 1/10, 2015 at 7:31 Comment(0)
C
1

Calculate working days in a month from any date:

public function getworkd($mday)
{
    $dn = new DateTime($mday);
    $dfrom = $dn->format('Y-m-01');
    $dtill = $dn->format('Y-m-t');
    $df = new DateTime($dfrom);
    $dt = new DateTime($dtill);
    $wdays = 0;
    while($df<=$dt)
    {
        $dof= $df->format('D') ;
        if( $dof == 'Sun' || $dof == 'Sat' ) ; else $wdays++;
        $df->add(new DateInterval('P1D'));
    }
    return $wdays;
}
Catechu answered 31/10, 2017 at 13:19 Comment(0)
L
0

Find the last day and the weekday for the given month
then do a simple while loop like :-

$dates = explode(',', date('t,N', strtotime('2013-11-01')));
$day = $dates[1]; 
$tot = $dates[0]; 
$cnt = 0;
while ($tot>1)
{   
    if ($day < 6)
    {   
        $cnt++;
    }   
    if ($day == 1)
    {   
        $day = 7;
    }   
    else
    {   
        $day--;
    }   
    $tot--;
}

$cnt = total of weekday (Monday to Friday) for a given month

Loiseloiter answered 6/12, 2011 at 16:49 Comment(1)
This code doesn't work as it should. It returns 20 days for Dec 2013, but it should 22.Meave
U
0

I've come up with a non-loop function. Much better in terms of performance. It might seem messy but it just needs to ask PHP the first day's weekday and the month's number days: the rest are arithmetical operations based on logic.

function countWorkDays($year, $month)
{
    $workingWeekdays   = 5;
    $firstDayTimestamp = mktime(0, 0, 0, $month, 1, $year);
    $firstDayWeekDay   = (int)date("N", $firstDayTimestamp); //1: monday, 7: saturday
    $upToDay           = (int)date("t", $firstDayTimestamp);

    $firstMonday = 1 === $firstDayWeekDay ? 1 : 9 - $firstDayWeekDay;
    $wholeWeeks  = $firstMonday < $upToDay ? (int)floor(($upToDay - $firstMonday + 1) / 7) : 0;
    $extraDays   = ($upToDay - $firstMonday + 1) % 7;

    $initialWorkdays      = $firstMonday > 1 && $firstDayWeekDay <= $workingWeekdays ? $workingWeekdays - $firstDayWeekDay + 1 : 0;
    $workdaysInWholeWeeks = $wholeWeeks * $workingWeekdays;
    $extraWorkdays        = $extraDays <= $workingWeekdays ? $extraDays : $workingWeekdays;

    return $initialWorkdays + $workdaysInWholeWeeks + $extraWorkdays;
}
Unreserve answered 2/2, 2018 at 17:4 Comment(0)
E
0

These functions work Without Loops.

The functions calculate the number of weekdays using:

  • day-number of first monday in month
  • number of days in month
// main functions 
// weekdays in month of year
function calculateNumberOfWeekDaysAtDate($month, $year)
{
    // I'm sorry, I don't know the right format for the $month and $year, I hope this is right.
    // PLEASE CORRECT IF WRONG
    $firstMondayInCurrentMonth = (int) date("j", strtotime("first monday of 01-$month-$year")); //get first monday in month for calculations
    $numberOfDaysOfCurrentMonth = (int) date("t", strtotime("01-$month-$year")); // number of days in month

    return calculateNumberOfWeekDaysFromFirstMondayAndNumberOfMonthDays($firstMondayInCurrentMonth, $numberOfDaysOfCurrentMonth);
}

// week days in current month
function calculateNumberOfWeekDaysInCurrentMonth()
{
    $firstMondayInCurrentMonth = (int) date("j", strtotime("first monday of this month")); //get first monday in month for calculations
    $numberOfDaysOfCurrentMonth = (int) date("t"); // number of days in this month

    return calculateNumberOfWeekDaysFromFirstMondayAndNumberOfMonthDays($firstMondayInCurrentMonth, $numberOfDaysOfCurrentMonth);
}

// helper functions
function calculateNumberOfWeekDaysFromFirstMondayAndNumberOfMonthDays($firstMondayInCurrentMonth, $numberOfDaysOfCurrentMonth)
{
    return $numberOfWeekDays = (($start = ($firstMondayInCurrentMonth - 3)) < 0 ? 0 : $start) + floor(($numberOfDaysOfCurrentMonth - ($firstMondayInCurrentMonth - 1)) / 7) * 5 + (($rest = (($numberOfDaysOfCurrentMonth - ($firstMondayInCurrentMonth - 1)) % 7)) <= 5 ? $rest : 5);
}
Estimable answered 11/4, 2018 at 14:51 Comment(0)
G
0
function workingDays($m,$y) {
    $days = cal_days_in_month(CAL_GREGORIAN, $m, $y);
    $workig_days = 0;
    $days_rest = array(5,6); //friday,saturday
    for ( $d=1 ; $d < $days+1 ; $d++ ) {
        if ( !in_array(date("w",strtotime("{$d}-{$m}-{$y}")),$days_rest)  ) {
            $workig_days++;
        }
    }
    return $workig_days;
}
Guillory answered 22/5, 2019 at 11:50 Comment(0)
E
0

I created a simple function that takes the $first_day_of_month (week day like Sunday/Monday etc). You can find out the first day of month like this:

date('N', strtotime(date("01-m-Y")));

And using the $month_last_date which can be procured like this:

date("t");

Here is the function:

function workingDaysInMonth(int $first_day_of_month, int $month_last_date) : array
{
    $working_days = [];
    $day = $first_day_of_month;
    $working_day_count = 0;
    for ($i = 1; $i <= $month_last_date; $i++) {
        if ($day == 8) {
            $day = 1;
        }
        if (!($day == 6 || $day == 7)) {
            $working_day_count++;
            $working_days[$i] = $working_day_count;
        }
        $day++;
    }
    return $working_days;
}
Evildoer answered 26/4, 2021 at 17:25 Comment(0)
P
-1

this will work

// oct. 2013
$month = 10;

// loop through month days
for ($i = 1; $i <= 31; $i++) {

    // given month timestamp
    $timestamp = mktime(0, 0, 0, $month, $i, 2012);

    // to be sure we have not gone to the next month
    if (date("n", $timestamp) == $month) {

        // current day in the loop
        $day = date("N", $timestamp);

        // if this is between 1 to 5, weekdays, 1 = Monday, 5 = Friday
        if ($day == 1 OR $day <= 5) {

            // write it down now
            $days[$day][] = date("j", $timestamp);
        }
    }
}

// to see if it works :)
print_r($days);
Pickerelweed answered 21/9, 2013 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.