如何获取当月的天数?在 PHP 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3691142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:43:52  来源:igfitidea点击:

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

phpdate

提问by sebastian

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.

我想检查今天是否是这个月的最后一天,但我真的不知道怎么做。我想用php写它。

can you help?

你能帮我吗?

thanks, Sebastian

谢谢,塞巴斯蒂安

回答by malonso

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

可能有比这更优雅的解决方案,但您可以使用 php 的 date 函数:

$maxDays=date('t');
$currentDayOfMonth=date('j');

if($maxDays == $currentDayOfMonth){
  //Last day of month
}else{
  //Not last day of the month
}

回答by Alex Pliutau

Try to use this:

尝试使用这个:

date('t');

回答by nothingchen01

date('t');

日期('t');

or you may use

或者你可以使用

cal_days_in_month.

cal_days_in_month。

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

见这里:http: //php.net/manual/en/function.cal-days-in-month.php

回答by Ashwani Shukla

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

strtotime 提供了许多很棒的功能,所以先检查一下

回答by Branimir

Use datefunction:

使用日期函数:

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

回答by Dahab

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

回答by Pascut

Use php's function: cal_days_in_month

使用php的函数:cal_days_in_month

More details here

更多细节在这里

回答by Rutvik kakadiya

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

回答by AlejandroAlis

this is to find the days in any month:

这是查找任何月份中的天数:

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

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

回答by Acyra

Using the modern DateTime object class:

使用现代 DateTime 对象类:

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