douzao2992 2016-11-08 18:25
浏览 80
已采纳

从任何日期开始,使用PHP生成一年中每周的开始和结束日期

I recently came across an app https://entire.life which shows a calendar like the images below in which it shows a row of dots for each year of your life.

The row of dots is 52 dots wide, 1 dot per week for that rows year.

Each row is a year.

Each rows 1st dot week date starts on the week of your birth date and not on January 1st of the year. So the row of week dots is 1 year per row starting and ending on your birthdate week.

The last image below shows that week dots that are in the future of todays date are shown as a grey dot to indicate that these weeks have not occured yet.

Here is a live example from the images above to see dit in action https://entire.life/jason-davis


https://i.stack.imgur.com/ne07D.png enter image description here


This image below shows a form to add events for the current week dot that was clicked on.

https://i.stack.imgur.com/K5BUT.png enter image description here


This image shows how the week dots for future dates are grey.

https://i.stack.imgur.com/pYGAn.png enter image description here


QUESTION

Using PHP I would like to generate a calendar like this based on a users birthdate like the above shown app.

Here is a live example from the images above to see dit in action https://entire.life/jason-davis

Rules:

  • Generate a row of dots representing each week of the year
  • Generate a row of the above week year dots for 100 years worth of rows starting on the users birth date year.
  • The starting week dot for each row should start on the users birth date week and not on January 1st of the year.

Using PHP's DateTime functions how can I determine the start and end dates for each of the 52 weeks based on a users birth date?

My birthdate in the example here is April 21 1983 The 1st years week dots represent these dates based on that start date:

1st row of year dots
- 21 – 27 Apr 1983 - 0 years old
- 28 Apr – 4 May 1983 - 0 years old
- 5 – 11 May 1983 - 0 years old
- 12 – 18 May 1983 - 0 years old
- 19 – 25 May 1983 - 0 years old
- 26 May – 1 Jun 1983 - 0 years old
- 2 – 8 Jun 1983 - 0 years old
- 9 – 15 Jun 1983 - 0 years old
- 16 – 22 Jun 1983 - 0 years old
- 23 – 29 Jun 1983 - 0 years old
- ...
- ... - ...
- 29 Mar – 4 Apr 1984 - 0 years old
- 5 – 11 Apr 1984 - 0 years old
- 12 – 18 Apr 1984 - 0 years old
- 19 – 20 Apr 1984 - 0 years old

2nd row of year dots
- 21 – 27 Apr 1984 1 year old - 28 Apr – 4 May 1984 - 1 years old
- 5 – 11 May 1984 - 1 years old
- 12 – 18 May 1984 - 1 years old
- 19 – 25 May 1984 - 1 years old
- 26 May – 1 Jun 1984 - 1 years old
- 2 – 8 Jun 1984 - 1 years old
- 9 – 15 Jun 1984 - 1 years old
- 16 – 22 Jun 1984 - 1 years old
- 23 – 29 Jun 1984 - 1 years old
- ...
- ...
- ...
- 29 Mar – 4 Apr 1985 - 1 years old
- 5 – 11 Apr 1985 - 1 years old
- 12 – 18 Apr 1985 - 1 years old
- 19 – 20 Apr 1985 - 1 years old

  • 写回答

1条回答 默认 最新

  • douyunjiaok300404 2016-11-08 18:32
    关注

    There you go:

    <?php 
       $userBirthDate = "1990-10-02"; // You probably use data from database or POST or GET, I setted a static one just for the example 
       $wishedDate = new DateTime($userBirthDate); // Here, you can add any desired date as an argument to DateTime, by default, it'll take the current DateTime.
       // This is for 100 years
       for ($x = 0; $x < 100; $x++){
         echo '<hr>'; // Just so every year will be separated by a html horizontal bar
         $limit = 52; // 52 for a year, but you could do as much as you need right there
         for ($i = 0; $i < $limit; $i++){
           echo $wishedDate->format('Y-m-d H:i:s'); // Write the current date
           echo '<br />'; // Just to switch line
           $wishedDate->modify('+1 week'); // Add one week to the wished date 
         }
       }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写