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 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C