Free code

Get the first & last day of the month with PHP

This is a couple of rather simple functions that will return the first and last day of the current month. Of course, you could rig them up to return the first and last of any month, but that's up to you once you've got my code :).

These two little functions are greating in a situation where you need to create a date range of the entire current month.

Currently they return the date in the format of 04/01/2009 and 04/30/2009, but you could rig them up however you'd like by changing the date() format parameter.

Here's the PHP code for the functions:

function firstOfMonth() {
return date("m/d/Y", strtotime(date('m').'/01/'.date('Y').' 00:00:00'));

function lastOfMonth() {
return date("m/d/Y", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));

And here's how to use them:

$date_start = firstOfMonth();
$date_endร‚ย  = lastOfMonth();

Of course, firstOfMonth() is very simple. It just takes the current month, day 1, current year, and 12am as the time.

However, last of month isn't so simple, because you can't just plug in a default end day as you know the months vary in length. And you can't just make a simple array of 12 end dates, because that doesn't account for leap years. So, what this function does is takes the first day of next month, and subtracts a day! Ahh, there's beauty in the simplicity of it!



  1. return date("m/01/Y");

  2. Thanks for the nice post. That idea lead me to the algorithm that I need.

  3. Pretty simple functions using your method which maintain it as a date:

    function GetFDoM($dateThis) {
    // Function for returning the First Day of the Month for the test dateThis value
    $retVal = NULL;
    if (is_numeric($dateThis)) {
    $dateSoM = strtotime(date('Y',$dateThis) . '-' . date('m',$dateThis) . '-01');
    if (is_numeric($dateSoM)) {
    $retVal = $dateSoM;
    return $retVal;

    function GetLDoM($dateThis) {
    // Function for returning the Last Day of the Month for the test dateThis value
    $retVal = NULL;
    if (is_numeric($dateThis)) {
    $dateSoM = strtotime(date('Y',$dateThis) . '-' . date('m',$dateThis) . '-01');
    $dateCog = strtotime('+1 month',$dateSoM);
    $dateEoM = strtotime('-1 day',$dateCog );
    if (is_numeric($dateEoM)) {
    $retVal = $dateEoM;
    return $retVal;

  4. I've changed this code to get the last day of 3 months ago.

    $yesterday = date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime((date('m')-2).'/01/'.date('Y').' 00:00:00'))));

    This gives me 1970-02-01 when I use it (todays date: 01-04-2010

  5. First and last day of month can also be calculated in the following way:
    $start_of_month = mktime(00, 00, 00, date('m'), 01);
    $end_of_month = mktime(23, 59, 59, date('m')+1, 00);

  6. very useful functions, just what i was looking for

  7. Hey!! Very useful post. BTW, this is shortest code to get the to_date

    date('Y-m-d', strtotime($from_date.'+1 MONTH -1 SECOND')))

  8. Thanks man! I tray to use:
    //first&last day of month
    $from = getdate(strtotime("first day last month"));
    $to = getdate(strtotime("last day last month"));
    But on client serwer it's doesn't work correctly.

  9. You guys went to far…

    //ISO8601 Short
    $start = date("Y-m-t"); //t -> last day of month
    $end = date("Y-m-01"); //01 -> first day of month

    Just that simple.

  10. Really nice function.Thanks for sharing.

  11. thanks for the codes..:D

  12. // for last day of month
    return date("m/d/Y", mktime(0, 0, 0, date("m")+1, 1, date("Y")) – 1);

  13. you guys are great…great job and ty for the usefull post

  14. Thanks, good posting. I use this function in my task…

  15. the problem is we have to know the first day of the month is monday or sunday ….. this is the prob , i respect all posts but all are incorrect and unusefull ;).
    what we need is to convert this 01/08/2001 to (Monday, Sunday,…) or (0,1….)

  16. One more way guys without strtotime(), I don't know why I don't like it.
    ๐Ÿ™‚ much more complex way than Eric Corona proposed ๐Ÿ™‚

    date('Y-m-d H:i:s', mktime(0, 0, -1, $currentMonth, 1, $currentYear))

  17. PHP Code: First and Last Day of Month | Gregory A McMullen

    […] post: Get the first & last day of the month with PHP Share This: This entry was posted in Coding and tagged code, dates, last day of month, php, […]

  18. Just wanted to say thanks. Even a few years later, the code is still relevant!

Leave a Reply

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

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image