您好我已经创建了一个数组,其开始日期增加了一个月,而这发生在有多少个月。因此,如果我的StartDate是10/10/15并将3放入我的for循环中,我会收到10/10/15 10/11/15 10/12/15 10/01/16。我目前的问题是,我添加了一个新的while循环,以查明这几个月中的某些日子是在星期六还是星期日。我不希望我希望它在工作日落下。所以香港专业教育学院创建了这个循环,但它有效,但我有另一个问如果10/11/15在一个星期天落下我的循环会将它改为星期一11/11/15,这是正确的,但其后的其余日期现在将遵循更改日期示例为11/12 / 15,11 / 01 / 16。
我希望它更改日期,但其他日期仍然从startdate(10/10/15)增加,如 10/10 / 15,11 / 11 / 15,10 / 12 / 15,10 / 1月16日即可。我该怎么做?我也理解我的一些代码可能不是最好的,但我只是一个初学者。任何帮助将不胜感激。
$date = "2015-10-1"; //startdate
$lol = 2; //length of loan
$start = strtotime($date); //startdate
$currentdate = $start;
echo "<pre>";
$times_table = [];
$titles[] = [
'Version' => 'Version 5 ',
'Date' => 'Feb-16',
'Name' => 'gary',
'RandNum' => '80',
'PaymentType' => 'P',
'dtType' => 'Q',
];
$times_table = [];
for($i = 0; $i <= ($lol + 1) ; $i++){
$times_table[$i]['StartDate']= $date ;
$times_table[$i]['Version']= "v10" ;
$cur_date = date("M-y-D", $currentdate);
$times_table[$i]['DtMy']= "<strong>" . $cur_date . "</strong>" ;
$mthYr = date("M", $currentdate);
$nxtmth = " ";
// Loop that changes the day to a weekday
while($nxtmth != "Y" ) {
if( $cur_date = date("M", $currentdate) !== $mthYr){
$nxtmth = "Y";
$currentdate = strtotime('-1 days', $currentdate);
$cur_date = date("d-M", $currentdate);
$times_table[$i]['DtMy']= "<strong>" . $cur_date . "</strong>" ;
}
if ($cur_date = date("w", $currentdate) == 0 || $cur_date = date("w", $currentdate) == 6){
if ($nxtmth == "Y"){
$currentdate = strtotime('-1 day', $currentdate);
$cur_date = date("d-M", $currentdate);
$times_table[$i]['DtMy']= "<strong>" . $cur_date . "</strong>" ;
}
else{
$currentdate = strtotime('+1 day', $currentdate);
$cur_date = date("d-M", $currentdate);
$times_table[$i]['DtMy']= "<strong>" . $cur_date . "</strong>" ;
}
if(($cur_date = date("M", $currentdate)) !== $mthYr){
$nxtmth = "Y";
$currentdate = strtotime('-1 day', $currentdate);
$cur_date = date("d-M", $currentdate);
$times_table[$i]['DtMy']= "<strong>" . $cur_date . "</strong>" ;
}
}
if($cur_date = date("w", $currentdate) > 0 && $cur_date = date("w", $currentdate) < 6 ){
$cur_date = date("d-M", $currentdate);
$times_table[$i]['DtMy']= "<strong>" . $cur_date . "</strong>" ;
break;
}
}
$currentdate = strtotime('+1 month', $currentdate); //Adds 1 month
}
print_r($times_table);
echo "</ pre>";
答案 0 :(得分:1)
您可以在覆盖
之前将值存储在temp变量中<?php
$date = "2015-10-1"; //startdate
$lol = 2; //length of loan
$dates = [];
for($i = 0; $i <= ($lol + 1) ; $i++){
$rawDate = strtotime($date);
$tmpRawDate = $rawDate;
$day = date('N', $rawDate);
// check if date is sat or sun
if($day == 6 || $day == 7)
$rawDate = strtotime($date . ' +'.(8 - $day).' day');
$dates[] = date('Y-m-d', $rawDate);
$rawDate = $tmpRawDate;
$rawDate = strtotime($date . ' +1 month');
$date = date('Y-m-d', $rawDate);
}
echo '<pre>';
print_r($dates);
<强>更新强>
<?php
$date = "2015-10-1"; //startdate
$lol = 2; //length of loan
$start = strtotime($date); //startdate
$currentdate = $start;
echo "<pre>";
$titles[] = [
'Version' => 'Version 5 ',
'Date' => 'Feb-16',
'Name' => 'gary',
'RandNum' => '80',
'PaymentType' => 'P',
'dtType' => 'Q',
];
$times_table = [];
for($i = 0; $i <= ($lol + 1) ; $i++){
// store the start date in tmp variable.
$tmpStart = $start;
// get the day counter 0 for monday .. 6 for sunday
$day = date('N', $start);
$times_table[$i]['StartDate']=$date;
$times_table[$i]['Version']="v10";
// check for sat and sun, if found increment to next monday
if($day == 6 || $day == 7)
$start = strtotime(' +'.(8 - $day).' day', $start);
$times_table[$i]['DtMy']= "<strong>" . date("d-M", $start) . "</strong>";
// restore the original variable
$start = $tmpStart;
// Increment one month
$start = strtotime('+1 month', $start);
}
print_r($times_table);