开发者

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

开发者 https://www.devze.com 2023-01-15 13:03 出处:网络
i want to check if today is the last day of t开发者_JAVA百科he month, but i don\'t really know how.

i want to check if today is the last day of t开发者_JAVA百科he month, but i don't really know how. i want to write it in php.

can you help?

thanks, Sebastian


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
}


Try to use this:

date('t');


date('t');

or you may use

cal_days_in_month.

see here: http://php.net/manual/en/function.cal-days-in-month.php


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


Use date function:

if (date('t') == date('j'))
{
   ...
}


Use php's function: cal_days_in_month

More details here

cal_days_in_month(CAL_GREGORIAN, 8, 2003);


$date = new DateTime('last day of this month');
$numDaysOfCurrentMonth = $date->format('d');


this is to find the days in any month:

$days = intval(date('t', strtotime($desiredDate)));


     echo date('t'); /// it return last date of current month


And here it is, wrapped up as a function:

function is_last_day_of_month($timestamp = NULL) {
    if(is_null($timestamp))
        $timestamp = time();
    return date('t', $timestamp) == date('j', $timestamp);
}


Using the modern DateTime object class:

$date = new DateTime('last day of this month');
0

精彩评论

暂无评论...
验证码 换一张
取 消