Find last day of the month in PHP


Yday I was working with dates and got the situation to get the last day of the month so got this piece of code.

date(’Y-m-d’,strtotime(’-1 second’,strtotime(’+1 month’,strtotime(date(’m').’/01/’.date(’Y').’ 00:00:00′))));

And this piece of code will gv you the last day of the your corrent month. 

tags: , , ,

About these ads

58 thoughts on “Find last day of the month in PHP

  1. great! thx!

    to get the last month,
    date(‘Y-m-d’,strtotime(‘-1 second’,strtotime(date(‘m’).’/01/’.date(‘Y’).’ 00:00:00′)));

    the output will bre the last day of the month for last month.

  2. Just for the record -
    This here is a bit shorter and thus better readable:
    date(“Y-m-d”,mktime(0, 0, 0, (date(‘m’) + 1), 0, date(‘Y)))

  3. You could combine all those strtotime functions:
    strtotime(‘+1 month -1 second ‘.date(‘Y’).’-’.date(‘m’).’-01′ );

  4. Thank you.. thank you.. date(’t’) returns only date of the last month. But I was looking for code that return complete date for last day of the month(ex – 2000-01-31) and found it here – I used tobi’s code –
    date(“Y-m-d”,mktime(0, 0, 0, (date(‘m’) + 1), 0, date(‘Y’)))

  5. super funda yar thanks a lot i using this i have formulated many calculation on date
    here is the find last month

    if(date(“m”)==1)
    {
    $to_month= 12 ;
    $to_year=date(“Y”)-1;
    }
    else
    {
    $to_month=date(“m”)-1;
    $to_year=date(“Y”);
    }

    $totime = date(“Y-m-d”,strtotime(“-1 second”,strtotime(“+1 month”,strtotime($to_month.”/01/”.$to_year.” 00:00:00″)))).”00:00:00″;;

  6. Pingback: Get the first and the last day of the current month in PHP | Svetoslav Marinov's Blog

  7. all this seems overly complex when ‘mktime(0,0,0,date(“m”) + 1, 0, date(“Y”)));’ does the same thing. Less function calls and slightly easier to read.

    mktime is very powerful, changing years or months if one value causes the them to roll over. date(“Y-m-d’,mktime(0,0,0, 2,32,2008)) will com out as 2008-03-03

  8. I know this is old….But I cant believe i was struggling to get here:

    $where[] = ‘ed1.date BETWEEN “‘.date(‘Y-m-01′, $date_ts).’” AND “‘.date(‘Y-m-t’, $date_ts).’”‘;

    “t” seems so obvious now – I was trying to find some weird shortcut like “last day of month” for strtotime() to gobble up. Anyway, this post may now die in peace.

  9. Wow! date(‘t’) is the simplest way
    before i use very very hard method:
    $m = date(‘m’);
    $y = date(‘Y’);

    //get number of days in current month
    $n = ($m!=2?(($m%2)^($m>7))+30:(!($y%400)||!($y%4)&&($y%25)?29:28));
    $n – is the number of the last day in month!!!

  10. Not so sure about date(‘t’);

    It looks good but

    date(‘D Y-m-t’, strtotime(“last month”) gives me Sun 2010-05-31

    but 2010-05-31 is a Monday

    • it works ok, but you are using it wrong. “D” and “t” are not referring to the same day.

      D: returns the day of the month, one month ago from now (i.e. if current date is Wed 07 Jul 2010, it returns Mon 07 Jun 2010), but “t” returns the number of days of the previous month (i.e. 30, as june has 30 days).

      (Y: returns the year of the previous month, m: returns a numeric representation of the previous month )

  11. From Php manual …. uses day “0″ , quite nice

    Example #3 Last day of next month
    The last day of any given month can be expressed as the “0″ day of the next month, not the -1 day. Both of the following examples will produce the string “The last day in Feb 2000 is: 29″.

  12. Hi, I am using this code:
    date(‘Y-m-t’, strtotime(‘now’) ); // 2010-11-30 = last day of this month
    date(‘Y-m-t’, strtotime(‘+1 month’) ); // 2010-12-31 = last day of next month
    etc.

  13. I needed, last day of a month, related to any date….

    $b=”2011-1-05″; // any date
    $c=1; // months after that date
    echo date(“Y-m-t”,mktime(0,0,0,date(“m”,strtotime($b))+$c,1,date(“Y”,strtotime($b))));
    // result: 2011-02-28

    • One more way without strtotime(), I don’t know why I don’t like it.

      - It will give you the last day/second of previous month.
      date(‘Y-m-d H:i:s’, mktime(0, 0, -1, $currentMonth, 1, $currentYear))

      - It will give you the last day of current month
      date(‘Y-m-t’, time())

  14. Pingback: My 2010 in review – by wordpress | <!– something random just like junk – yard –>

  15. If you want to print the th, st with the last day of the month within your date function.
    Like June 30th, 2011 or July 31st, 2011
    Try this:
    date(‘F tS, Y’,strtotime(date(‘F t’)))

  16. Pingback: Get the last day of the month - Logan Bibby

  17. Pingback: Code PHP สำหรับค้นหาวันสุดท้ายของเดือน | Web Development and Programming Help.

  18. getting start date and end date of last month.

    $dt = date(‘Y-m-d’);
    $date_start = date(‘Y-m’, strtotime(‘-1 month’. $dt)) . ‘-01′;
    $date_end = date(‘Y-m-t’, strtotime($date_last_1_month_start));

    of course the above can be changed to fit any date.

  19. getting start date and end date of last month.

    $dt = date(‘Y-m-d’);
    $date_start = date(‘Y-m’, strtotime(‘-1 month’. $dt)) . ‘-01′;
    $date_end = date(‘Y-m-t’, strtotime($date_start));

    of course the above can be changed to fit any date.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s