<!– something random just like junk – yard –>

Find last day of the month in PHP

Posted in LAMP, learning by ganu on February 28, 2007

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

17 Responses

Subscribe to comments with RSS.

  1. Pramod said, on September 5, 2007 at 11:11 am

    Simply Great Script.

    How do it for the last month one

  2. sugger502 said, on September 19, 2007 at 1:43 pm

    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. Natasha said, on September 24, 2007 at 11:51 am

    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. Tobi said, on October 15, 2007 at 7:38 pm

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

  5. Syam Kumar said, on October 22, 2007 at 3:41 pm

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

  6. jdswift said, on June 24, 2008 at 11:37 am

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

  7. Vitor S. said, on July 30, 2008 at 8:43 pm

    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. Johan said, on August 8, 2008 at 12:01 pm

    I agree this is much faster and readable

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

  9. Henri said, on August 13, 2008 at 2:30 pm

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

  10. kalyan said, on August 20, 2008 at 1:57 am

    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. Alit Atmaja said, on September 19, 2008 at 12:27 pm

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

    Cheers,
    alita

  12. sham said, on September 26, 2008 at 2:27 pm

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

    thank you for good code

  14. Ajit said, on December 4, 2008 at 11:10 am

    ThanX for the functional bindings.

  15. Knowledge Chikuse said, on March 4, 2009 at 3:10 pm

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

  16. porter said, on June 11, 2009 at 2:40 am

    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


Leave a Reply