在PHP中查找上个月的最后一天

时间:2022-01-09 07:51:01

I'm a little unsure why this is not able to find the last day of the previous month. Every steps seems to be working correctly, except when the final date is created.

我有点不确定为什么这不能找到上个月的最后一天。除非创建最终日期,否则每个步骤似乎都正常工作。

<?php

$currentMonth = date('n');
$currentYear = date('Y');

if($currentMonth == 1) {
    $lastMonth = 12;
    $lastYear = $currentYear - 1;
}
else {
    $lastMonth = $currentMonth -1;
    $lastYear = $currentYear;
}

if($lastMonth < 10) {
    $lastMonth = '0' . $lastMonth;
}

$lastDayOfMonth = date('t', $lastMonth);

$lastDateOfPreviousMonth = $lastYear . '-' . $lastMonth . '-' . $lastDayOfMonth;

$newLastDateOfMonth = date('F j, Y', strtotime($lastDateOfPreviousMonth));

?>

$lastDateOfPreviousMonth is returning 2012-09-30 as expected; however, after trying to convert it to September 30, 2012 - $newLastDateOfMonth is returning October 1, 2012. Where do I seem to be going wrong?

$ lastDateOfPreviousMonth按预期返回2012-09-30;然而,在尝试将其转换为2012年9月30日之后 - $ newLastDateOfMonth将于2012年10月1日返回。我似乎哪里出错了?

EDIT: If using date("t/m/Y", strtotime("last month")); or date('Y-m-d', strtotime('last day of previous month')); will either of these still be viable given 2013-01-01, i.e. will they account for the change in year?

编辑:如果使用日期(“t / m / Y”,strtotime(“上个月”));或日期('Y-m-d',strtotime('上个月的最后一天'));鉴于2013-01-01,其中任何一项仍然可行,即它们是否会影响年度的变化?

4 个解决方案

#1


60  

echo date('Y-m-d', strtotime('last day of previous month'));
//2012-09-30

or

要么

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

Later edit: php.net documentation - relative formats for strtotime(), DateTime and date_create()

稍后编辑:php.net文档 - strtotime(),DateTime和date_create()的相对格式

#2


19  

There is a php function for this.

这有一个PHP功能。

echo date("t/m/Y", strtotime("last month"));

#3


4  

First day of this month, minus 1 second.

本月的第一天,减去1秒。

echo date('Y-m-d',strtotime('-1 second',strtotime(date('m').'/01/'.date('Y'))));

Example here.

这里的例子。

#4


0  

You can use strtotime()'s zero handling functionality to achieve this also:

您可以使用strtotime()的零处理功能来实现此目的:

# Day Before
    echo date('Y-m-d', strtotime('2016-03-00')); // 2016-02-29

# Year can be handled too
    echo date('Y-m-d', strtotime('2016-01-00')); // 2015-12-31

# Month Before
    echo date('Y-m-d', strtotime('2016-00-01')); // 2015-12-01

# Month AND Day
    echo date('Y-m-d', strtotime('2016-00-00')); // 2015-11-30

Makes sense if you think of 00 as 'one less than the first (01)'.

如果你认为00比第一个(01)少一个,那就更有意义了。

So to achieve the objective of this question, 'last day of previous month' is a simple case of

所以为了实现这个问题的目标,“上个月的最后一天”是一个简单的例子

date('your_format', strtotime('YYYY-ThisMonth-00'));
# So:
date('Y-m-d', strtotime('2016-11-00')); // 2016-10-31

#1


60  

echo date('Y-m-d', strtotime('last day of previous month'));
//2012-09-30

or

要么

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

Later edit: php.net documentation - relative formats for strtotime(), DateTime and date_create()

稍后编辑:php.net文档 - strtotime(),DateTime和date_create()的相对格式

#2


19  

There is a php function for this.

这有一个PHP功能。

echo date("t/m/Y", strtotime("last month"));

#3


4  

First day of this month, minus 1 second.

本月的第一天,减去1秒。

echo date('Y-m-d',strtotime('-1 second',strtotime(date('m').'/01/'.date('Y'))));

Example here.

这里的例子。

#4


0  

You can use strtotime()'s zero handling functionality to achieve this also:

您可以使用strtotime()的零处理功能来实现此目的:

# Day Before
    echo date('Y-m-d', strtotime('2016-03-00')); // 2016-02-29

# Year can be handled too
    echo date('Y-m-d', strtotime('2016-01-00')); // 2015-12-31

# Month Before
    echo date('Y-m-d', strtotime('2016-00-01')); // 2015-12-01

# Month AND Day
    echo date('Y-m-d', strtotime('2016-00-00')); // 2015-11-30

Makes sense if you think of 00 as 'one less than the first (01)'.

如果你认为00比第一个(01)少一个,那就更有意义了。

So to achieve the objective of this question, 'last day of previous month' is a simple case of

所以为了实现这个问题的目标,“上个月的最后一天”是一个简单的例子

date('your_format', strtotime('YYYY-ThisMonth-00'));
# So:
date('Y-m-d', strtotime('2016-11-00')); // 2016-10-31