dongqiao8502 2018-06-20 07:38
浏览 41
已采纳

不包括2个月(日期)

I added a condition to my function. I would like if the new date ($dateSelect) falls between the 01 of July and the 30 of August that it shifts for the month of September and I would also exclude Sundays, if it falls on a Sunday it shifts to Saturday. That's what I already did:

function calcDate($date)
{
    $dateSelect = date('Y-m-d', strtotime('+30 days', strtotime($date)));
}

Do you have any idea how I can do that?

Thank you.

  • 写回答

1条回答 默认 最新

  • douyou7102 2018-06-20 07:48
    关注

    You can use date("m") to find out what month it is in the new calculated date.
    If it is within the period move it to september.

    function calcDate($date)
    {
        $m = date('m', strtotime('+30 days', strtotime($date)));
        if($m >= 7 && $m< 9){
            $dateSelect = date('Y-09-d', strtotime($date));   
        }else{
            $dateSelect = date('Y-m-d', strtotime('+30 days', strtotime($date)));
        }
        $N =  date("N", strtotime($dateSelect));
    
        if($N >6) $dateSelect = date("Y-m-d", strtotime($dateSelect . " previous Saturday"));  
    
        return $dateSelect;
    }
    
    echo calcDate("2018-07-30"); // should return 30, but thats's a sunday. So 2018-09-29
    

    https://3v4l.org/QSCvL

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备