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!

  • 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.

    • David Hancock

      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?

  • Hi David. This post has really been helpful. How about the first and last days of previous week, previous month, previous year?

    • I have put together the logic to find previous dates. I can share it here if you would like it.

      • 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.

  • Loic

    I believe in firstDayOf() function, the condition ‘monday last week’ : ‘monday this week’ is wrong.
    When i pass the date 1st of January 2017 to the function firstDayOf(‘week’, $date), it returns the 19th of december 2016.
    So, personnally i simply changed this line to: $newDate->modify(‘monday this week’);
    and it seems to work well. Thanks for this.