所以,我有这个功能:
function calculateNextDate($startDate, $days)
{
$dateTime = new DateTime($startDate);
while($days) {
$dateTime->add(new DateInterval('P1D'));
if ($dateTime->format('N') < 6) {
$days--;
}
}
return $dateTime->format('Y-m-d');
}
如果我像这样运行我的功能:
echo calculateNextDate('2012-10-01', '10');
这将导致:
2012-10-15
哪个是正确的,但是我希望将$startDate
计为计算日,所以它会像这样计算:
1. 2012-10-01
2. 2012-10-02
3. 2012-10-03
4. 2012-10-04
5. 2012-10-05
6. 2012-10-08
7. 2012-10-09
8. 2012-10-10
9. 2012-10-11
10. 2012-10-12
有可能吗?
答案 0 :(得分:1)
<?
function calculateNextDate($startDate, $days)
{
$dateTime = new DateTime($startDate);
while($days > 0) {
$weekend = date('w', strtotime($dateTime->format('Y-m-d')));
if($weekend != '6' && $weekend != '0'){
$new_date[] = $dateTime->format('Y-m-d');
$days--;
}
$dateTime->add(new DateInterval('P1D'));
}
return $new_date;
}
echo "<pre>";
print_r(calculateNextDate('2012-10-11', '10'));
?>
结果将是:
Array
(
[0] => 2012-10-11
[1] => 2012-10-12
[2] => 2012-10-15
[3] => 2012-10-16
[4] => 2012-10-17
[5] => 2012-10-18
[6] => 2012-10-19
[7] => 2012-10-22
[8] => 2012-10-23
[9] => 2012-10-24
)
您可以通过循环播放数组来回显日期。
答案 1 :(得分:1)
首先减去一天
function calculateNextDate($startDate, $days)
{
$oneDay = new DateInterval('P1D');
$dateTime = new DateTime($startDate);
$dateTime->sub($oneDay);
while($days) {
$dateTime->add($oneDay);
if ($dateTime->format('N') < 6) {
$days--;
}
}
return $dateTime->format('Y-m-d');
}
echo calculateNextDate('2012-10-01', 10);
// results in 2012-10-12
答案 2 :(得分:0)
这是一个非常复杂的方式,只需添加一天开始日期
我会像这样做
$resultingDate = date('Y-m-d', strtotime("+$days days", $startDate));