PHP计算指定日期之前的天数PLUS将天数添加到一周的指定日期

时间:2018-04-29 23:17:08

标签: php math

我对PHP计数功能有疑问。这是我想要完成的事情。

我有一个名为'出发日期'另一个名为"付款到期'。我想计算从今天开始到出发日期的那一天,并将此记录在' Payment Due In'。

这很简单。但是,我还要求在星期三付款。

这意味着如果出发日期是星期日,则总和将是: 算今天() - >到出发日期的时间+到星期三的天数=到付款的天数。

如果可以,请帮助我!

提前致谢。

更新

这是我的代码块:

<?php 
while($row4 = mysqli_fetch_array($search_res)){  //shows result of qry

$datetimeFromMysql = $row4['arrival_date'];
$newTime = strtotime($datetimeFromMysql);
$myFormatForView = date("d/m/Y", $newTime);

$datetimeFromMysql2 = $row4['departure_date'];
$newTime2 = strtotime($datetimeFromMysql2);
$myFormatForView2 = date("d/m/Y", $newTime2); 

//以英文日期格式显示此到达/离开日期

$DepartureDate = $row4['departure_date'];
$today = new DateTime();
$depd = new DateTime($DepartureDate);

$interval = $today->diff($depd);
$days = (int)$interval->format('%a');


// adjust to make payment due on a Wednesday
$dow = (int)$depd->format('N');
$days += ($dayOF - $dow) %7;
?>

// $ dayOF允许用户在通常付款时更改Mond(1) - Sun /(7)

    <tr>
        <td><?php echo $row4['reference_no'];?></td>
        <td><?php echo $myFormatForView?></td>
        <td><?php echo $myFormatForView2?></td>
        <?php
            echo "<form method='post' action='hostPayments.php?id=" . $row4['group_id'] ."'>" 
        ?>
        <?php if ($days == '0'){ $class="bg-warning"; } else if($depd >= $today){ $class="bg-success"; } else{ $class="bg-danger"; } ?>

        <td class="<?php echo $class; ?>"><b><?php if($days == '0'){?> TODAY! <?php } else {  echo $days; ?></b> Days <?php if($depd >= $today){ ?> Time <?php } else { ?> Ago <?php }} ?></td>     
        <td><?php echo $row4['status']; ?></td>
        <td class="danger"><button type="submit" name="host_payment" class="btn btn-warning"><span class="glyphicon glyphicon-search"></span></button> <b> - NOT COMPLETE</b> <span style="color: white" class="glyphicon glyphicon-exclamation-sign"></span></form></td>


    </tr>
<?php
}

如果付款将来到期,则该字段为绿色(bg-success)并输出$ days +单词&#39; Time&#39;阅读(7天时间)

如果付款延迟,则该字段为红色(bg-danger)并输出$ days +单词&#39; Ago&#39;阅读(3天前)

如果今天付款,则该字段为黄色(bg警告)和“今天”字样。显示

  • 在出发日期为5月9日的条目中,输出为6天时间&#39; (我已经改变了通常现在付款到周二)。系统计算到出发日期之前的下一个星期二,而不是下周二。

1 个答案:

答案 0 :(得分:1)

这应该做你想要的。首先,它根据出发日期和付款到期的星期几来确定付款到期日。然后计算付款到期前的天数(或逾期金额)。注意我已经包含了一些您可能想要删除的调试打印。

X

输出(基于今天=&#39; 2018-04-30&#39;):

function payment_due_date($DepartureDate, $dayOf) {
    $depd = new DateTime($DepartureDate);
    // adjust to make payment due on the specified day of the week
    // 1=Monday, 2=Tuesday,...,7=Sunday
    $dow = (int)$depd->format('N');
    $depd->add(new DateInterval('P' . ((7 + $dayOf - $dow) % 7) . 'D'));
    echo 'Payment due date is: ' . $depd->format('Y-m-d') . "\n";
    return $depd;
}

function payment_due_days($DepartureDate, $dayOf) {
    // get payment due date
    $depd = payment_due_date($DepartureDate, $dayOf);
    $today = new DateTime();
    // need to set time to 0 to match $DepartureDate
    $today->setTime(0,0);
    $interval = $today->diff($depd);
    $days = (int)$interval->format('%r%a');
    if ($days == 0)
        echo "Payment is due today\n";
    else if ($days > 0)
        echo "Payment is due in $days days\n";
    else
        echo "Payment was due " . abs($days) . " days ago\n";
    return $days;
}

payment_due_days('2018-05-01', 2);
payment_due_days('2018-05-01', 4);
payment_due_days('2018-05-09', 2);
payment_due_days('2018-04-20', 2);
payment_due_days('2018-05-18', 3);
payment_due_days('2018-06-02', 4);

请注意我们使用Payment due date is: 2018-05-01 Payment is due in 1 days Payment due date is: 2018-05-03 Payment is due in 3 days Payment due date is: 2018-05-15 Payment is due in 14 days Payment due date is: 2018-04-24 Payment was due 6 days ago Payment due date is: 2018-05-23 Payment is due in 23 days Payment due date is: 2018-06-07 Payment is due in 38 days 的原因是,如果值为负,则PHP会从(7 + $dayOf - $dow) % 7返回负数。否则我们会使用%。这就是您的代码在5月9日的出发日期给出错误的截止日期的原因。