为了账号安全,请及时绑定邮箱和手机立即绑定

根据例外情况计算和调整日期 (PHP)

根据例外情况计算和调整日期 (PHP)

PHP
犯罪嫌疑人X 2023-11-03 17:41:51
规则如下:天数的添加始终为15 秒(例如 15、30、45、60 等)当到期日为15 日或月底(例如,30 或 31 取决于月份,每年 2 月 28 或 29 取决于闰年)时,添加天数(如上所述)时,日期应仅落在15 日或月底。当到期日不是每 15 日或月底时,通常只需添加天数。当日期为2 月 14 日并添加 15 天时,如果是闰年,则应返回02/29;如果不是闰年,则应返回 02/28。这是我的代码,但是我收到 1 个错误和不一致。当日期为02/29/2020并添加30 天时,可捕获致命错误。我可以做什么来适应这个规则?
查看完整描述

1 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

function adjustDate($maturitydate, $add) {

    $nodays = '+'.$add.' days';

    $date = new DateTime($maturitydate);

    $matdt = $date->modify($nodays);

    $ismaturitydateendofmonth = check_end_of_month($maturitydate);


    if($date->format('d') == 15) {

        $matdt =  $matdt->format('m/15/Y');

    }

    else if($ismaturitydateendofmonth == '1'){

        $matdt->modify('last day of this month');

    }

    else{

        $matdt =  $matdt->format('m/d/Y');

    }

    

    return $matdt;

}


function check_end_of_month($date){

    //adds 1 day to date

    $Temp = date('m/d/Y',strtotime("+1 day", strtotime($date)));


    //get the month of each date

    $tempmonth = date('m', strtotime($Temp));

    $datemonth = date('m', strtotime($date));

    

    //check if the months are equal

    if($tempmonth != $datemonth){

        return '1';

    }

    else{

        return '0';

    }

}


查看完整回答
反对 回复 2023-11-03
  • 1 回答
  • 0 关注
  • 61 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信