0

我正在尝试创建一个程序,在付款到期日通知我。我几乎知道它是如何完成的,但仍然存在一个问题。假设客户在 2017 年 1 月 31 日开设了账户。下一个到期日是 2/28/18(一个月后)。我的代码指出,如果今天的日期少于保单开启的日期,则不要将其计为一个月(因为还没有过去整整一个月),因此即使它应该算作整月也不会.

这段代码几乎适用于所有日期,除了 2 月的最后一天,或者该月在 30 日结束并且保单在 31 日开放。

$today = time();        
$policy_opened = strtotime($policy_opened);
                
//number of months the policy has been opened                             
$year_opened = date('Y',$policy_opened);
$year_now = date('Y',$today);               
$month_opened = date('m',$policy_opened);
$month_now = date('m',$today);
$day_opened = date('d',$policy_opened );
$day_now = date('d',$today);
$months_since_opened = (($year_now - $year_opened) * 12) + ($month_now - $month_opened);
$months_since_opened = $months_since_opened + 1; //since it can't start with 0 months opened.
if($day_now < $day_opened){
    //this is in order to count it as one month only if a 'whole month' has passed by.
    $months_since_opened = $months_since_opened - 1;
}
                
4

1 回答 1

0

您可以尝试使用本机 Datetime 函数进行直接 mont 或 day 差异计算,这里是月份计算的代码。这也给你两个日期的月份差异

$policyOpen = "2017-12-01";
$toDay = "2017-12-31";

$smallDate = new DateTime($opened);
$bigDate = new DateTime($toDay);

$months_since_opened= $small->diff($big)->format("%m");



you can also get diffrent date related parameter as shown below
echo $diff->y; //this will give you year diffrence
echo $diff->m; //this will give you month diffrence
echo $diff->d; //this will give you day diffrence
echo $diff->h; //this will give you hours diffrence
echo $diff->i; //this will give you minute diffrence
echo $diff->s; //this will give you second diffrence
于 2017-11-28T11:05:45.147 回答