好吧,我有以下代码:它会在给定日期之前返回所有日期基础,直到do-while
循环结束。
$paid = 2600;
$monthly = 700;
$x=0;
do {
$monthly;
$date = date('2016'.'-'.'05'.'-d');
$time = strtotime($date);
$bill_month = date("Y-m-d", strtotime("+$x month", $time));
echo "$bill_month => $monthly <br/>";
$paid -= $monthly;
if ($paid < $monthly) {
$monthly = $paid;
}
$x++;
} while ($paid > 0);
它返回以下结果:
2016-05-12 => 700
2016-06-12 => 700
2016-07-12 => 700
2016-08-12 => 500
我还有另一个array
如下:
$date_array = array("2016-05-12", "2016-07-12");
现在我想使用上面do-while loop
显示以下日期:
2016-06-12
2016-08-12
2016-09-12
2016-10-12
我的意思是:
你知道上面的while循环返回所有下一个日期,直到循环结束。例如:从给定日期开始的下一个4个月。
但我希望在接下来的4个月内展示$date_array
中不存在的内容。这意味着这4个月:
2016-06-12
2016-08-12
2016-09-12
2016-10-12
答案 0 :(得分:1)
只需检查生成的内容是否在$date_array
,如果是下个月:
$found = false;
while (!$found) {
$date = date('2016'.'-'.'05'.'-d');
$time = strtotime($date);
$bill_month = date("Y-m-d", strtotime("+$x month", $time));
$found = !in_array($bill_month,$date_array);
++$x;
}
echo "$bill_month => $monthly <br/>";
并删除while循环中的++$x
清洁,如果
$bill_month = date("Y-m-d",strtotime(($x++) . " month"),$time));
完整示例:
<?php
/**
*/
$paid = 2600;
$monthly = 700;
$date_array = array("2016-05-12", "2016-07-12");
$x=0;
do {
$found = false;
while (!$found) {
$date = date('2016'.'-'.'05'.'-d');
$time = strtotime($date);
$bill_month = date("Y-m-d", strtotime( ($x++)." month", $time));
$found = !in_array($bill_month,$date_array);
}
echo "$bill_month => $monthly <br/>\n";
$paid -= $monthly;
if ($paid < $monthly) {
$monthly = $paid;
}
} while ($paid > 0);