Get number of weekdays in a given month

2019-01-12 07:14发布

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 ?

11条回答
We Are One
2楼-- · 2019-01-12 07:22

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;
}
查看更多
我命由我不由天
3楼-- · 2019-01-12 07:23

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.

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-12 07:24

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);
}
查看更多
一纸荒年 Trace。
5楼-- · 2019-01-12 07:25

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;
}
查看更多
聊天终结者
6楼-- · 2019-01-12 07:27

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;
}
查看更多
老娘就宠你
7楼-- · 2019-01-12 07:29

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;
}
查看更多
登录 后发表回答