如何在PHP中获得每月的最后一天?
考虑到:
$a_date = "2009-11-23"
我想要2009-11-30;鉴于
$a_date = "2009-12-23"
我要2009-12-31。
如何在PHP中获得每月的最后一天?
考虑到:
$a_date = "2009-11-23"
我想要2009-11-30;鉴于
$a_date = "2009-12-23"
我要2009-12-31。
当前回答
这是一个更优雅的表达月底的方式:
$thedate = Date('m/d/Y');
$lastDayOfMOnth = date('d', mktime(0,0,0, date('m', strtotime($thedate))+1, 0, date('Y', strtotime($thedate))));
其他回答
您也可以将它与datetime一起使用
$date = new \DateTime();
$nbrDay = $date->format('t');
$lastDay = $date->format('Y-m-t');
T返回给定日期当月的天数(date参见文档):
$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));
如果你知道一个月的最后一天,
public function getLastDateOfMonth($month)
{
$date = date('Y').'-'.$month.'-01'; //make date of month
return date('t', strtotime($date));
}
$this->getLastDateOfMonth(01); //31
试试这个,如果你使用的是PHP 5.3+,
$a_date = "2009-11-23";
$date = new DateTime($a_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');
为了查找下个月最后的日期,修改如下:
$date->modify('last day of 1 month');
echo $date->format('Y-m-d');
等等。
你的解决方案在这里。
$lastday = date('t',strtotime('today'));