“Last month” incorrect with PHP Date & strtotime functions

I use a lot of business style reporting in our companies website where displaying figures by month is important. However, at certain points in the year using the PHP date and strtotime functions to pull data from our DB works incorrectly. For example, today and tomorrow (30th & 31st March), using “last month” in the strtotime function will actually return the timestamp for 2nd & 3rd March, not the 27th & 28th February that the developer may have been expecting.

The reason for this? It’s because the Romans were superstitious of even numbers and thus we have a very disorganised calendar system that doesn’t always match up with the decimal and binary way we now work in.

If you do want to get the timestamp of a date that falls in the previous month, the below code should help:

$last = strtotime(date(‘Y-m-01′).’ -1 month’);

This will (at the moment) return the 1st February.

 

Leave a Reply

You must be logged in to post a comment.