how to get the number of days of the current month? in PHP

后端 未结 11 1889
悲哀的现实
悲哀的现实 2021-02-12 03:35

i want to check if today is the last day of the month, but i don\'t really know how. i want to write it in php.

can you help?

thanks, Sebastian

相关标签:
11条回答
  • 2021-02-12 03:58

    In order to get no. of days in month you can use either

    date('t')
    

    OR

    cal_days_in_month(CAL_GREGORIAN, 8, 2003)
    

    And if you wish to check if today is the last day of month us can use

    if(date('d')==date('d',strtotime('last day of month'))){
    //your code
    }
    

    strtotime offers many great features so check them out first

    0 讨论(0)
  • 2021-02-12 04:01
         echo date('t'); /// it return last date of current month
    
    0 讨论(0)
  • 2021-02-12 04:04

    Try to use this:

    date('t');
    
    0 讨论(0)
  • 2021-02-12 04:04

    Use php's function: cal_days_in_month

    More details here

    0 讨论(0)
  • 2021-02-12 04:09

    There is probably a more elegant solution than this but you can just use php's date function:

    $maxDays=date('t');
    $currentDayOfMonth=date('j');
    
    if($maxDays == $currentDayOfMonth){
      //Last day of month
    }else{
      //Not last day of the month
    }
    
    0 讨论(0)
  • 2021-02-12 04:11

    Using the modern DateTime object class:

    $date = new DateTime('last day of this month');
    
    0 讨论(0)
提交回复
热议问题