Home > LAMP, learning > Find last day of the month in PHP

Find last day of the month in PHP

February 28, 2007 Leave a comment Go to comments

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: , , ,

  1. September 5, 2007 at 11:11 am | #1

    Simply Great Script.

    How do it for the last month one

  2. sugger502
    September 19, 2007 at 1:43 pm | #2

    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. September 24, 2007 at 11:51 am | #3

    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. October 15, 2007 at 7:38 pm | #4

    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
      May 24, 2011 at 12:58 am | #5

      Shorter != better readable

    • October 14, 2011 at 6:43 pm | #6

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

    • Maxilou
      March 22, 2012 at 10:33 pm | #7

      Thanks for this!

  5. October 22, 2007 at 3:41 pm | #8

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

    • Binu V Pillai
      February 3, 2011 at 4:48 pm | #9

      Slight modificatiion of your answere

      For getting last date of any month

      date(“t”,$tm);

      where $tm is the time stamp of the date

    • August 21, 2011 at 11:14 pm | #11

      incredibly simple, thank you!

  6. jdswift
    June 24, 2008 at 11:37 am | #12

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

  7. July 30, 2008 at 8:43 pm | #13

    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. August 8, 2008 at 12:01 pm | #14

    I agree this is much faster and readable

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

  9. Henri
    August 13, 2008 at 2:30 pm | #16

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

    • Ken
      June 14, 2011 at 5:12 am | #17

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

  10. August 20, 2008 at 1:57 am | #18

    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. September 19, 2008 at 12:27 pm | #19

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

    Cheers,
    alita

  12. sham
    September 26, 2008 at 2:27 pm | #20

    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
    November 26, 2008 at 9:31 am | #21

    thank you for good code

  14. December 4, 2008 at 11:10 am | #22

    ThanX for the functional bindings.

  15. Knowledge Chikuse
    March 4, 2009 at 3:10 pm | #23

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

  16. porter
    June 11, 2009 at 2:40 am | #24

    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
    January 20, 2010 at 7:34 am | #25

    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. March 3, 2010 at 3:49 pm | #26

    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. May 14, 2010 at 5:44 pm | #27

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

  20. Bob
    June 23, 2010 at 2:37 pm | #28

    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
    June 23, 2010 at 2:39 pm | #29

    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
      July 23, 2010 at 2:39 pm | #30

      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. July 21, 2010 at 4:06 pm | #31

    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
    August 23, 2010 at 9:47 pm | #32

    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
    September 2, 2010 at 1:12 pm | #33

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

  25. October 29, 2010 at 4:19 am | #34

    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. November 1, 2010 at 12:01 pm | #35

    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
    January 3, 2011 at 7:33 pm | #36

    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
      September 16, 2011 at 4:11 pm | #37

      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
        January 18, 2012 at 11:45 pm | #38

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

  28. January 19, 2011 at 12:37 am | #39

    $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. March 22, 2011 at 12:12 am | #40

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

  30. jay
    April 5, 2011 at 4:59 pm | #41

    Many thanks for this time saving code.

  31. Christopher Jones
    June 3, 2011 at 9:24 pm | #42

    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. June 22, 2011 at 1:44 pm | #43

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

  33. June 30, 2011 at 2:26 pm | #44

    Thanks 4 the script :D

  34. July 11, 2011 at 4:43 pm | #45

    Great post. ty for the script

  35. October 31, 2011 at 3:09 am | #46

    Nice PHP trick, works fine for me!

  36. November 10, 2011 at 4:19 pm | #47

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

  37. November 10, 2011 at 4:20 pm | #48

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

  38. kc-viper
    January 11, 2012 at 7:53 pm | #49

    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
    January 11, 2012 at 7:54 pm | #50

    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. March 13, 2012 at 3:37 pm | #51

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

  41. Amit
    April 18, 2012 at 2:23 pm | #52

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

  42. ummi hasya
    May 4, 2012 at 12:18 am | #53

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

  1. May 22, 2009 at 1:13 am | #1
  2. January 7, 2011 at 6:53 pm | #2
  3. December 18, 2011 at 8:23 pm | #3
  4. December 22, 2011 at 3:14 pm | #4

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.