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

54 Comments

  1. Posted September 5, 2007 at 11:11 am | Permalink | Reply

    Simply Great Script.

    How do it for the last month one

  2. sugger502
    Posted September 19, 2007 at 1:43 pm | Permalink | Reply

    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.

  3. Posted September 24, 2007 at 11:51 am | Permalink | Reply

    Hi

    I was thinking to write a complete function and then googled your this post. I cant believe you did it with one line.

    great work thank you.

    Natasha

  4. Posted October 15, 2007 at 7:38 pm | Permalink | Reply

    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)))

    • info@mailinator.com
      Posted May 24, 2011 at 12:58 am | Permalink | Reply

      Shorter != better readable

    • Posted October 14, 2011 at 6:43 pm | Permalink | Reply

      Many times more efficient as well, date and mktime are highly optimised functions, strtotime is not.

    • Maxilou
      Posted March 22, 2012 at 10:33 pm | Permalink | Reply

      Thanks for this!

  5. Posted October 22, 2007 at 3:41 pm | Permalink | Reply

    date(‘t’) is enough to get the last day of the month.

    • Binu V Pillai
      Posted February 3, 2011 at 4:48 pm | Permalink | Reply

      Slight modificatiion of your answere

      For getting last date of any month

      date(“t”,$tm);

      where $tm is the time stamp of the date

    • Posted August 21, 2011 at 11:14 pm | Permalink | Reply

      incredibly simple, thank you!

  6. jdswift
    Posted June 24, 2008 at 11:37 am | Permalink | Reply

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

  7. Posted July 30, 2008 at 8:43 pm | Permalink | Reply

    kumar is right to get the last day of the current month is just date(‘t’) you don’t need to use more function, remember keep it simple :)

  8. Posted August 8, 2008 at 12:01 pm | Permalink | Reply

    I agree this is much faster and readable

    date(“Y-m-t”); //t = number of days in the month

  9. Henri
    Posted August 13, 2008 at 2:30 pm | Permalink | Reply

    cal_days_in_month() return days of month. Very handy, because format and year are parameters.

    • Posted June 14, 2011 at 5:12 am | Permalink | Reply

      Good one. Exactly what I’m looking for. Simple, readable. Thanks.

  10. Posted August 20, 2008 at 1:57 am | Permalink | Reply

    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’)))

  11. Posted September 19, 2008 at 12:27 pm | Permalink | Reply

    Thanks Guys… This Post just help me out to find the parameter t. I’m using date(‘t’).

    Cheers,
    alita

  12. sham
    Posted September 26, 2008 at 2:27 pm | Permalink | Reply

    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″;;

  13. khao_lek
    Posted November 26, 2008 at 9:31 am | Permalink | Reply

    thank you for good code

  14. Posted December 4, 2008 at 11:10 am | Permalink | Reply

    ThanX for the functional bindings.

  15. Knowledge Chikuse
    Posted March 4, 2009 at 3:10 pm | Permalink | Reply

    pooleez, just use date(‘Y-m-t’)
    why all the complications

  16. porter
    Posted June 11, 2009 at 2:40 am | Permalink | Reply

    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

  17. Sean
    Posted January 20, 2010 at 7:34 am | Permalink | Reply

    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.

  18. Posted March 3, 2010 at 3:49 pm | Permalink | Reply

    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!!!

  19. Posted May 14, 2010 at 5:44 pm | Permalink | Reply

    easy peasy
    date(‘t’,strtotime(“last month”));

  20. Bob
    Posted June 23, 2010 at 2:37 pm | Permalink | Reply

    Not so sure about date(‘t’);

    It looks good but

    gives me

    Sun 2010-05-31

    but 2010-05-31 is a Monday

  21. Bob
    Posted June 23, 2010 at 2:39 pm | Permalink | Reply

    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

    • vagkavan
      Posted July 23, 2010 at 2:39 pm | Permalink | Reply

      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 )

  22. Posted July 21, 2010 at 4:06 pm | Permalink | Reply

    To get the last day of a month use this quite simple code:

    $month =2;
    echo date(‘Y-m-t’,mktime(0,0,0,$month,1,2010));

  23. pon
    Posted August 23, 2010 at 9:47 pm | Permalink | Reply

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

  24. Cold
    Posted September 2, 2010 at 1:12 pm | Permalink | Reply

    $date = new Zend_Date();
    $lastday = $date->get(Zend_Date::MONTH_DAYS);

  25. Posted October 29, 2010 at 4:19 am | Permalink | Reply

    You saved my butt with this one! Apparently it’s pretty easy to do in 5.3 but not so much in 5.2

  26. Posted November 1, 2010 at 12:01 pm | Permalink | Reply

    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.

  27. Jorge
    Posted January 3, 2011 at 7:33 pm | Permalink | Reply

    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

    • Paul
      Posted September 16, 2011 at 4:11 pm | Permalink | Reply

      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())

      • Charles
        Posted January 18, 2012 at 11:45 pm | Permalink | Reply

        … not working related to ANY DATE… that’s the problem.

  28. Posted January 19, 2011 at 12:37 am | Permalink | Reply

    $month = date(“m”);
    $year = date(“Y”);
    date(“t”, mktime(1,1,1,$month,1,$year));

    This code returns the last day from a given month ;)

  29. Posted March 22, 2011 at 12:12 am | Permalink | Reply

    I think date(‘Y-m-t’) will do the job.

  30. Posted April 5, 2011 at 4:59 pm | Permalink | Reply

    Many thanks for this time saving code.

  31. Christopher Jones
    Posted June 3, 2011 at 9:24 pm | Permalink | Reply

    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’)))

  32. Posted June 22, 2011 at 1:44 pm | Permalink | Reply

    great job…i will realy appreciat if you can help me with understanding this script. TY very much

  33. Posted June 30, 2011 at 2:26 pm | Permalink | Reply

    Thanks 4 the script :D

  34. Posted July 11, 2011 at 4:43 pm | Permalink | Reply

    Great post. ty for the script

  35. Posted October 31, 2011 at 3:09 am | Permalink | Reply

    Nice PHP trick, works fine for me!

  36. Posted November 10, 2011 at 4:19 pm | Permalink | Reply

    Just simple code to Find last day of the month in PHP

  37. Posted November 10, 2011 at 4:20 pm | Permalink | Reply

    echo date(‘Y-m-d’,strtotime(“last Day”));

  38. kc-viper
    Posted January 11, 2012 at 7:53 pm | Permalink | Reply

    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.

  39. kc-viper
    Posted January 11, 2012 at 7:54 pm | Permalink | Reply

    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.

  40. Posted March 13, 2012 at 3:37 pm | Permalink | Reply

    $Totaldaysforlastmonth = date(“t”, mktime(0,0,0, date(“n”) – 1));

  41. Amit
    Posted April 18, 2012 at 2:23 pm | Permalink | Reply

    you can also do this
    date(‘Y-m-t’);
    the ‘t’ will get the last day of the month

  42. ummi hasya
    Posted May 4, 2012 at 12:18 am | Permalink | Reply

    or this :)
    echo date(‘t’, strtotime(“2012-02-01”) ); // return 29

  43. morfie
    Posted May 16, 2012 at 7:18 pm | Permalink | Reply

    date(‘Y-m-d H:i:s’, mktime(23,59,59,$month+1,0,$year));

4 Trackbacks

  1. […] https://ullu.wordpress.com/2007/02/28/find-last-day-of-the-month-in-php/ […]

  2. […] Find last day of the month in PHP February 2007 29 comments 4 […]

  3. By Get the last day of the month - Logan Bibby on December 18, 2011 at 8:23 pm

    […] into an issue where I needed to know the last day of the month in PHP. Sure enough, there’s a way. date('Y-m-d',strtotime('+1 […]

  4. […] https://ullu.wordpress.com/2007/02/28/find-last-day-of-the-month-in-php/ Posted in PHP Tags: PHP You can leave a response, or trackback from your own site. […]

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: