Joel
Joel

Reputation: 2824

PHP seems to be returning incorrect weekdays

First of all, here is my code:

<?php
$year = 1006;
while ($year <= 1996)
{
    $date = '1/26/' . $year;
    if (date('l', strtotime($date)) == 'Monday')
    {
        echo $date . PHP_EOL;
    }

    $year += 10;
}
?>

This is supposed to output the January 26s between 1000 and 1999 where the year ends with a 6 and the date falls on a Monday. Here is the output:

1/26/1006
1/26/1096
1/26/1136
1/26/1186
1/26/1226
1/26/1316
1/26/1366
1/26/1406
1/26/1496
1/26/1536
1/26/1586
1/26/1626
1/26/1756
1/26/1846
1/26/1976

All seems well. However, when I look at few of these years on timeanddate.com, the weekdays do not seem to have anything in common:

1/26/1006 falls on a Saturday

1/26/1406 falls on a Tuesday

1/26/1756 does fall on a Monday

What's going on here?

Upvotes: 0

Views: 133

Answers (1)

Sam Dufel
Sam Dufel

Reputation: 17608

strtotime() converts a string representation of a date into a unix timestamp - the number of seconds since jan 1, 1970. You may start to see the problem here...

A timestamp is typically stored in a 32 bit integer, which gives a range between 1970 and 2038. Trying to generate timestamps for dates outside that range will give you undefined behavior.

-- edit --

From the php manual:

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).

Upvotes: 3

Related Questions