我正在研究一个小支付系统,必须生成一个付款日(每月)列表,给出初始日期和付款次数。例如:
给定:
startday: 2015/06/22
qtty: 6
我应该从初始日期(22)中获取日期,并生成一个包含6个连续月日期的列表:
- 2015/06/22(如果应该包括初始日期,并且大于今天)
- 2015/07/22
- 2015/08/24
- 2015/09/22
- 2015/10/22
- 2015/11/23
可以看到,生成的日期不应该是周末(sat/dom),如果可能的话,也不应该是节假日
有什么功能可以帮助我实现这一点吗?TIA
我想这可能会做你想要的,包括假期:
<?php
$startday = strtotime("2015/08/24");
$qtty = 5;
// Add as many holidays as desired.
$holidays = array();
$holidays[] = "4 Jul";
$holidays[] = "25 Dec";
for( $i = 0; $i < $qtty; $i++ ) {
$next_month = strtotime("+".$i." month", $startday); // Also works with "+ 30 days"
while( in_array(date("d M", $next_month), $holidays)) { // Is holiday
$next_month = strtotime("+ 1 day", $next_month);
if( date("N", $next_month) > 5 ) { // Is weekend
$next_month = strtotime("next Monday", $next_month); // or "previous Friday"
}
}
echo(date( "Y-m-d", $next_month) . '</br>');
}
?>
将回声
2015-08-25
2015-09-25
2015-10-26 // Because of weekend
2015-11-25
2015-12-28 // Because of Christmas and weekend
如果起始日期为2015/10/31,输出将为:
2015-11-02 // Because of weekend
2015-12-01 // Because the 31st of Nov is the 1st of Dec
2015-12-31
2016-02-01 // Because the weekend
2016-03-02 // Because the 31st of Feb is the 2st of Mars (as 2016 is leep year)
作为一个很好的额外提示,根据你想如何解决1月31日的问题,如果你想要每个月的最后一天,你总是可以使用以下方法:
$first_of_the_month = strtotime(date("Y-m-1", $startday));
$next_month = strtotime("+ 1 month", $first_of_the_month);
$last_of_the_month = date("Y-m-t", $next_month);
echo($last_of_the_month); // 2015-09-30