Php to find my birthday, where the day of the name "Monday" is another 100 years

I was born on 1986-04-21, which is Monday. My next birthday with the name of the day is Monday - 1997-04-21, etc.

I wrote a program to find up to 100 years old to find out what year my birthday goes with the corresponding day name that is on Monday.

This is the code:

<?php date_default_timezone_set('Asia/Calcutta'); for($year = 1986; $year < 2086; $year++) { $timestamp = mktime(0, 0, 0, 4, 21, $year); if(date('l', $timestamp) == 'Monday') { echo date('Ymd, l', $timestamp) . "\n"; } } ?> 

This is the output of the program:

 1986-04-21, Monday 1997-04-21, Monday 2003-04-21, Monday 2008-04-21, Monday 2014-04-21, Monday 2025-04-21, Monday 2031-04-21, Monday 2036-04-21, Monday 

Now my problem is why PHP does not support until 1970 and after 2040.
So, how can I get a birthday after 2040 or before 1970?

+4
source share
3 answers

There is no need to use any special classes or date processing functions.

Your birthday is after a February leap day, so from one year to the next it will be one day (365% 7) or (in leap years) two days (366% 7) later in the week than it was a year earlier.

 $year = 1985; // start year $dow = 0; // 0 for 1985-04-21 (Sunday) while ($year < 2100) { $year++; $dow++; if ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)) { $dow++; // leap year } $dow %= 7; // normalise back to Sunday -> Saturday if ($dow == 1) { printf("%04d-%02d-%02d is a Monday\n", $year, 4, 21); } } 

This code will work with any version of PHP.

+6
source

If you are using PHP 5.3, you can use the DateTime class and add DateInterval s. It is based on 64-bit integers and has no problem in 2038 .

Basic example:

 <?php $year = DateInterval::createFromDateString('1 year'); $date = new DateTime('1986-04-21'); $date->add($year); echo $date->format('Ym-d') . "\n"; // Repeat 100 times 

documentation on createFromDateString() here .

+5
source

For this reason, you cannot go through 1970 or last 2038 in a date guide :

The valid timestamp range is usually from Friday, December 13, 1901. 20:45:54 GMT - Tuesday, January 19, 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values ​​for a 32-bit subscription integer). However, before PHP 5.1.0, this range was limited from 01-01-1970 to 19-01-2038 on some systems (for example, Windows).

+1
source

All Articles