Getting the first day of the previous month in PHP

Attention: Les informations de ce billet sont susceptibles d'être obsolètes car vieux de plus 2 ans.

Warning: The information you are reading may be obsolete, this post was published more than 2 years ago.

Today a colleague of mine came to me with a simple question.

How to get the first day of the previous month in PHP ?

My colleague was using the following code:

<?php
echo date('Y-m-01', strtotime('-1 MONTH')), PHP_EOL;

But the code would not work and would instead return the first day of the current month. This behavior is documented in the PHP documentation so after some quick thinking here’s the code we ended up using:

<?php
echo  date('Y-m-d', strtotime(date('Y-m-01').' -1 MONTH')), PHP_EOL;

We first detect the first day of the current month and then we substract 1 month.

Now to be completely honest, this code sucks but we are still going to use it because it will run on any PHP version. But If you are using a version of PHP>=5.3 you should know that the parsing algorithm has been improved. So next time you really need this information use the following code:

<?php
date_default_timezone_set('UTC'); //should be set in your script or in your php.ini
$date = new DateTime('FIRST DAY OF PREVIOUS MONTH');
echo $date->format('Y-m-d'), PHP_EOL;

One thought on “Getting the first day of the previous month in PHP

  1. What can cause sucks to you about this ?

    Your code even have typo error echo date(‘Y-m-01’, strtotime(‘-1 MONTH’), PHP_EOL;

    It’s can definitely cause error. I think there is no problem at all and just un pretty way of writing.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.