获得本月的最后一天?

gre*_*emo 24 php datetime

可能重复:
PHP的最后一天

有没有想任何功能$date->getMonthDays()$date->getLastDayOfMonth()在PHP中获得的天在给定月份数(或最后一天数)?

$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'));
Run Code Online (Sandbox Code Playgroud)

编辑:谢谢,投票结束,这是重复,抱歉...

小智 70

php日期函数为您提供't'的月份天数

date("t");
Run Code Online (Sandbox Code Playgroud)

请参阅:http://php.net/manual/en/function.date.php


Mar*_*jnG 26

上个月约会很简单

echo date("Y-m-t", strtotime("-1 month") ) ;
echo date("Y-m-1", strtotime("-1 month") ) ;
Run Code Online (Sandbox Code Playgroud)

3月3日回归

2011-02-28
2011-02-1
Run Code Online (Sandbox Code Playgroud)


Bra*_*tie 5

t为您提供当月的总天数.j为您提供当月的当天.

使用modifyformat日期时间的一些减法,你可以到月底.

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