April 18th, 2009

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!

Share

18 Responses to ' Get the first & last day of the month with PHP '

Subscribe to comments with RSS or TrackBack to ' Get the first & last day of the month with PHP '.

  1. soho said,

    on May 14th, 2009 at 9:56 am

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

  2. 1mr3yn said,

    on September 29th, 2009 at 2:17 am

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

  3. Amaranthe said,

    on December 28th, 2009 at 5:22 pm

    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. darow said,

    on January 4th, 2010 at 5:01 am

    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. suvasish said,

    on January 7th, 2010 at 3:50 am

    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. blestab said,

    on April 14th, 2010 at 5:42 am

    very useful functions, just what i was looking for

  7. codef0rmer said,

    on August 26th, 2010 at 6:09 am

    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. Dominik said,

    on November 8th, 2010 at 5:41 am

    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. Erik Corona said,

    on November 26th, 2010 at 10:49 pm

    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. Nikunj said,

    on May 19th, 2011 at 2:43 am

    Really nice function.Thanks for sharing.

  11. blue_blods said,

    on May 29th, 2011 at 10:50 pm

    thanks for the codes..:D

  12. Anthony said,

    on June 17th, 2011 at 1:38 am

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

  13. Ohana said,

    on June 22nd, 2011 at 3:11 am

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

  14. sobat_rahmad said,

    on August 4th, 2011 at 11:12 pm

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

  15. brahimhackman said,

    on August 11th, 2011 at 8:12 am

    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. Paul said,

    on September 16th, 2011 at 5:33 am

    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. on September 19th, 2011 at 10:43 am

    [...] 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. on September 19th, 2011 at 10:45 am

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

Leave a reply

*
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.
Click to hear an audio file of the anti-spam word