2014-03-27 86 views
0

我正在用Php開發考勤系統。如何動態計算每個月的工作日數?

我必須計算除假期外的每個月的總辦公室工作日。

如果有人請假,意味着個人工作日應在辦公室工作日減1。

我該怎麼做?

+0

請提供您的代碼。 –

+0

歡迎來到Stackoverflow。你到目前爲止嘗試過什麼? –

+1

完全取決於你的辦公室,如果它在所有標準場合允許假期。 – aelor

回答

1

請試試看,我認爲它會幫助你。

var get_working_days = function(year, month_number){ 
    var start_dt = new Date(year, month_number, 1); 
    var end_dt = null; 
    var count_sunday = 0; 

    /* these are moonth number on based of index like 0 for jan, 1 for feb etc*/ 
    var month_31 = [0,2,4,6,7,9,11]; 
    var month_30 = [3,5,8,10]; 
    var month_feb = [1]; 

    if(month_31.indexOf(month_number) >= 0){ 
    end_dt = new Date(year, month_number, 31); 
    }else if(month_30.indexOf(month_number) >= 0){ 
    end_dt = new Date(year, month_number, 30); 
    }else if(month_feb.indexOf(month_number) >= 0){ 
    if(year%4 == 0){ 
     end_dt = new Date(year, month_number, 29); 
    }else{ 
     end_dt = new Date(year, month_number, 28); 
    } 
    } 
    var number_of_days = end_dt.getDate(); 
    while(number_of_days >= 7){ 
    count_sunday += 1; 
    number_of_days = number_of_days - 7; 
    } 
    if(number_of_days > 0){ 
    if(start_dt.getDay()+number_of_days >= 7){ 
     count_sunday += 1;  
    } 
    } 
    var working_days = end_dt.getDate() - count_sunday; 
    return working_days; 
} 

var dt = new Date(); 
var year = dt.getFullYear(); 
var month_lst = ['jan', 'feb', 'mar', 'apr', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ] 
var month = dt.getMonth()+1; /*pass number 1 ,2 3 ... for month on the based of indes like 0 for january*/ 
for(var count = 0 ; count < month_lst.length; count++){ 
    var working_days = get_working_days(year, count) 
    console.log(" Working days of "+month_lst[count] +" " + year +" are : "+ working_days); 
} 

請在圖片上看看更多的理解。

enter image description here

相關問題