doulu7921 2018-06-12 05:22
浏览 33
已采纳

PHP DateTime:7天之间的下一个星期二

I hope that someone can help me out. I am trying to build a dropdown menu, which shows the next 4-6 tuesdays with the correct date. This is already working. Problem now is, that I need the following:

If today is wednesday, the dropdown should start with the first tuesday BUT not from next week, instead with the tuesday from the week after next week. Why? Because I always need full 7 days between the registration and the delivery time.

So if today is monday, the next (first) tuesday should be the tuesday from next week. Also lets say today is friday, then my dropdown should not start with the tuesday from next week, instead with the tuesday from the week after next week.

Something like: Today is Monday - tomorrow will be tuesday but there are not 7 days between so the tuesday from next will should be shown.

Here is what I have so far:

$begin = new DateTime('tuesday this week');
$end = new DateTime('last tuesday of next month');

$interval = new DateInterval( 'P1W' );
$daterange = new DatePeriod( $begin, $interval ,$end );

<select class='form-control margin-bottom-20' name='delivery_first' required>
    foreach($daterange as $date){
        <option value='".$date->format('d.m.Y')."'>"; echo $date->format('l'); echo $date->format('d.m.Y'); print"</option>
    }
</select>

Any idea how I can implement the check, if 7 days are between? Hope my question is clear. If not please let me know.

  • 写回答

5条回答 默认 最新

  • doucou19961205 2018-06-12 05:39
    关注

    You could use a minimum datetime instance to compare against.

    $begin = new DateTime('next tuesday');
    $min = new DateTime('+7 days');
    $interval = new DateInterval( 'P1W' );
    
    if ($begin < $min) {
        $begin->add($interval);
    }
    

    However, if you always want two tuesdays from now, this simple change might work:

    $begin = new DateTime('next tuesday +7 days');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部