php 获取本月的最后一天?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8912780/
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-26 05:46:57  来源:igfitidea点击:

Get the last day of the month?

phpdatetime

提问by gremo

Possible Duplicate:
PHP last day of the month

可能的重复:
PHP 本月的最后一天

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)?

是否有类似$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'));

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

编辑:谢谢,投票关闭,这是重复的,抱歉问...

回答by Preau

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

php date 函数为您提供带有 't' 的月份中的天数

date("t");

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

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

回答by MartijnG

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

3 月 3 日返回

2011-02-28
2011-02-1

回答by Brad Christie

tgives you the total number of days in the current month. jgives you the current day of the month.

t给你当月的总天数。j给你当月的当前日期。

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

使用modify并从format-ing 日期时间中减去一些,您可以到月末。

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