JasonDavis
JasonDavis

Reputation: 48963

Generate start and end dates for each week of a year starting from any date with 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.sstatic.net/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.sstatic.net/K5BUT.png enter image description here


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

https://i.sstatic.net/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:

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

Upvotes: 2

Views: 215

Answers (1)

Yann Chabot
Yann Chabot

Reputation: 4869

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 
     }
   }
?>

Upvotes: 2

Related Questions