php 如何从日期中找到一个月的最后一天?

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

How to find the last day of the month from date?

phpdate

提问by Mithun Sreedharan

How can I get the last day of the month in PHP?

如何在 PHP 中获取本月的最后一天?

Given:

鉴于:

$a_date = "2009-11-23"

I want 2009-11-30; and given

我要 2009-11-30; 并给予

$a_date = "2009-12-23"

I want 2009-12-31.

我想要 2009-12-31。

回答by Dominic Rodger

treturns the number of days in the month of a given date (see the docs for date):

t返回给定日期的月份中的天数(请参阅文档date):

$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));

回答by Mugunth

The code using strtotime() will fail after year 2038. (as given in the first answer in this thread) For example try using the following:

使用 strtotime() 的代码将在 2038 年之后失败。(如该线程中的第一个答案所示)例如尝试使用以下内容:

$a_date = "2040-11-23";
echo date("Y-m-t", strtotime($a_date));

It will give answer as: 1970-01-31

它将给出答案:1970-01-31

So instead of strtotime, DateTime function should be used. Following code will work without Year 2038 problem:

所以应该使用 DateTime 函数而不是 strtotime。以下代码将在没有 2038 年问题的情况下工作:

$d = new DateTime( '2040-11-23' ); 
echo $d->format( 'Y-m-t' );

回答by Hannoun Yassir

I know this is a little bit late but i think there is a more elegant way of doing this with PHP 5.3+by using the DateTimeclass :

我知道这有点晚了,但我认为PHP 5.3+使用DateTime类有一种更优雅的方法:

$date = new DateTime('now');
$date->modify('last day of this month');
echo $date->format('Y-m-d');

回答by MPV

There is also the built in PHP function cal_days_in_month()?

还有内置的 PHP 函数cal_days_in_month()

"This function will return the number of days in the month of year for the specified calendar." http://php.net/manual/en/function.cal-days-in-month.

“此函数将返回指定日历一年中月份的天数。” http://php.net/manual/en/function.cal-days-in-month

echo cal_days_in_month(CAL_GREGORIAN, 11, 2009); 
// = 30

回答by kaleazy

This should work:

这应该有效:

$week_start = strtotime('last Sunday', time());
$week_end = strtotime('next Sunday', time());

$month_start = strtotime('first day of this month', time());
$month_end = strtotime('last day of this month', time());

$year_start = strtotime('first day of January', time());
$year_end = strtotime('last day of December', time());

echo date('D, M jS Y', $week_start).'<br/>';
echo date('D, M jS Y', $week_end).'<br/>';

echo date('D, M jS Y', $month_start).'<br/>';
echo date('D, M jS Y', $month_end).'<br/>';

echo date('D, M jS Y', $year_start).'<br/>';
echo date('D, M jS Y', $year_end).'<br/>';

回答by john Smith

What is wrong - The most elegant for me is using DateTime

有什么问题 - 对我来说最优雅的是使用 DateTime

I wonder I do not see DateTime::createFromFormat, one-liner

我不知道我没有看到DateTime::createFromFormat,单行

$lastDay = \DateTime::createFromFormat("Y-m-d", "2009-11-23")->format("Y-m-t");

回答by nikc.org

You could create a date for the first of the next month, and then use strtotime("-1 day", $firstOfNextMonth)

您可以为下个月的第一天创建一个日期,然后使用 strtotime("-1 day", $firstOfNextMonth)

回答by Mohammed Safeer

Try this , if you are using PHP 5.3+,

试试这个,如果你使用的是 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');

For finding next month last date, modify as follows,

查找下个月的最后日期,修改如下,

 $date->modify('last day of 1 month');
 echo $date->format('Y-m-d');

and so on..

等等..

回答by Vineet Kadkol

Your solution is here..

您的解决方案在这里..

$lastday = date('t',strtotime('today'));

回答by Faisal

You can find last day of the month several ways. But simply you can do this using PHP strtotime()and date()function.I'd imagine your final code would look something like this:

您可以通过多种方式找到本月的最后一天。但是你可以简单地使用 PHP strtotime()date()函数来做到这一点。我想你的最终代码看起来像这样:

$a_date = "2009-11-23";
echo date('Y-m-t',strtotime($a_date));

Live Demo

现场演示

But If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:

但是如果您使用的是 PHP >= 5.2,我强烈建议您使用新的 DateTime 对象。例如像下面这样:

$a_date = "2009-11-23";
$date = new DateTime($a_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');

Live Demo

现场演示

Also, you can solve this using your own function like below:

此外,您可以使用自己的函数来解决这个问题,如下所示:

/**
 * Last date of a month of a year
 *
 * @param[in] $date - Integer. Default = Current Month
 *
 * @return Last date of the month and year in yyyy-mm-dd format
 */
function last_day_of_the_month($date = '')
{
    $month  = date('m', strtotime($date));
    $year   = date('Y', strtotime($date));
    $result = strtotime("{$year}-{$month}-01");
    $result = strtotime('-1 second', strtotime('+1 month', $result));

    return date('Y-m-d', $result);
}

$a_date = "2009-11-23";
echo last_day_of_the_month($a_date);