2014-01-17 179 views
2

我需要將2個工作日添加到當前日期。 我打算使用strtotime,但strtotime的平日不包括星期六。如何指定星期六爲星期幾作爲時間

$now = date("Y-m-d H:i:s"); 
$add = 2; 
$format = "d.m.Y"; 
if(date('H') < 12) { 
    $add = 1; 
} 
$date = strtotime($now . ' +'.$add.' weekdays'); 
echo date($format, $date); 

如果您在星期五運行它,則會輸出星期二。但它實際上應該在星期一返回。

我如何將週六添加爲工作日?

+0

我不認爲你可以做到。如果你需要特定的業務邏輯,你應該自己實現它,而不是依賴'strtotime'的automagic。 「營業日」的定義與「平日」不同。假期怎麼樣? – deceze

回答

2

獲取從特定日期+天數下一工作日偏移:

function get_next_business_date($from, $days) { 
    $workingDays = [1, 2, 3, 4, 5, 6]; # date format = N (1 = Monday, ...) 
    $holidayDays = ['*-12-25', '*-01-01', '2013-12-24']; # variable and fixed holidays 

    $from = new DateTime($from); 
    while ($days) { 
     $from->modify('+1 day'); 
     if (!in_array($from->format('N'), $workingDays)) continue; 
     if (in_array($from->format('Y-m-d'), $holidayDays)) continue; 
     if (in_array($from->format('*-m-d'), $holidayDays)) continue; 
     $days--; 
    } 
    return $from->format('Y-m-d'); # or just return DateTime object 
} 

print_r(get_next_business_date('today', 2)); 

demo

+1

非常感謝:) –

相關問題