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 
         }
       }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化