duan4523 2013-09-16 04:21
浏览 50
已采纳

PHP(while,date_diff)

I have a past date of appointment set. Every new appointment is 8 days apart from the previous (past appointment+8 days). I would like to create a simple script to return the dates of appointments for the next 365 days starting from the current day each time (i.e. 0=< interval (today-appointment) =< 365).

I've tried something like this but I don't get what I want:

  <?php 
    date_default_timezone_set('Europe/London');

    $today = new DateTime("now");
    $appointment=new DateTime('2013-08-26');
    $interval = $appointment->diff($today)->d; 

    while ($interval <= 365 && $interval => 0)
    {
       echo $appointment->format('l n F Y');
       $appointment->add(new DateInterval('P8D'));
    } ?>
  • 写回答

1条回答 默认 最新

  • dongqiyou0303 2013-09-16 09:58
    关注

    Sum from your question :

    1. starting next new appointment is first date from today, that is on +8 day interval from appointment in the past;
    2. every next appointment is +8 days from previous one;
    3. show appointments only between today and today+365days interval;
    date_default_timezone_set('Europe/London');
    
    $today = new DateTime('today');
    $appointment = new DateTime('2013-08-23');
    $next_appointment_in_days = ceil($appointment->diff($today)->days / 8) * 8;
    $next_appointment = clone $appointment;
    $next_appointment->modify("+$next_appointment_in_days day");
    
    do {
        echo $next_appointment->format('l, j F Y') . "
    ";
        $next_appointment->modify('+8 day');
    } while ($today->diff($next_appointment)->days <= 365);
    

    Run this code.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器