Get the First/Last Day of a Week, Month, Quarter or Year in PHP

If you’ve ever needed to find the first or last day of a given period and you’re rocking a PHP version greater than or equal to 5.2, today is your lucky day!

Both of these functions return a DateTime object, so you can output the date in any format available to the PHP date() function.

Get the First Day of a Week, Month, Quarter or Year

Get the Last Day of a Week, Month, Quarter or Year

Here is a usage example of both functions:

This is the output of the above usage example when run today:

I hope you find these functions useful!

5 thoughts on “Get the First/Last Day of a Week, Month, Quarter or Year in PHP

  1. Joe

    These functions don’t work correctly until PHP 5.3.6 which added support for absolute time intervals on DateTime::modify. Prior to that only relative changes were supported. source.

    Reply
    1. David Hancock Post author

      Thanks for your comment Joe. Are you sure they don’t work? They seem to work as expected for me on PHP 5.3.3. Can you possibly provide an example where they fail so I can take a look?

      Reply
      1. Vincent

        Shreerang, I think it would be great if you could share how you used these functions to find previous dates. I need to find, for example, the first and last days of last year.

        Reply

Leave a Reply

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