doulu7921 2018-06-12 13: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 13: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条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分