Get the last day of the month? [duplicate]

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

Possible Duplicate:
PHP last day of the month

Is there any function like $date->getMonthDays() or $date->getLastDayOfMonth() in PHP to get the number of days in a given month (or the last day number)?

$start = new DateTime('2012-02-01'); $end   = clone $start;  // Interval = last day of the month minus current day in $start $interval = $start->getLastDayOfMonth() - intval($start->format('j')); $end->add(new DateInterval('P' . $interval . 'D')); 

EDIT: thanks, voted to close, it's a duplicate, sorry for asking...

回答1:

The php date function gives you the number of days in the month with 't'

date("t"); 

See: http://php.net/manual/en/function.date.php



回答2:

It's simple to get last month date

echo date("Y-m-t", strtotime("-1 month") ) ; echo date("Y-m-1", strtotime("-1 month") ) ; 

at March 3 returns

2011-02-28 2011-02-1 


回答3:

t gives you the total number of days in the current month. j gives you the current day of the month.

Using modify and some subtraction from format-ing the datetime, you can get to the end of the month.

$date = new DateTime(); $lastDayOfMonth = $date->modify(   sprintf('+%d days', $date->format('t') - $date->format('j')) ); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!