我正在开发一个小额支付系统,并且必须根据初始日期和付款次数生成一个付款天数列表(每月)。所以,例如:
鉴于:
开始日:2015/06/22
qtty:6
我应该从最初的日期(22)开始,并生成一个包含6个连续月度日期的列表:
如您所见,生成的日期不应该是周末(坐/下)和 - 如果可能 - 也不是假期
是否有任何功能可以帮助我实现这一目标? TIA
答案 0 :(得分:1)
我认为这可能会做你想要的,包括假期:
<?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