在 PHP 中查找上个月的最后一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12674041/
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
Finding Last Day of Previous Month in PHP
提问by William Orazi
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));
?>
$lastDateOfPreviousMonthis returning 2012-09-30 as expected; however, after trying to convert it to September 30, 2012 - $newLastDateOfMonthis returning October 1, 2012. Where do I seem to be going wrong?
$lastDateOfPreviousMonth按预期返回 2012-09-30;但是,在尝试将其转换为 2012 年 9 月 30 日之后 - 将在 2012 年$newLastDateOfMonth10 月 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?
编辑:如果在 2013 年 1 月 1 日使用date("t/m/Y", strtotime("last month"));或date('Y-m-d', strtotime('last day of previous month'));这些中的任何一个仍然可行,即它们是否会说明年份的变化?
回答by Mihai Iorga
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() 的相关格式
回答by Lea
There is a php function for this.
有一个 php 函数。
echo date("t/m/Y", strtotime("last month"));
回答by wesside
回答by stalinrajindian
please try with below answer.
请尝试以下答案。
code:
代码:
echo date("t/m/Y", strtotime(date('Y-m')." -1 month"));
You will get last day of previous 12 months.
您将获得前 12 个月的最后一天。
Example:
例子:
<?php
for ($i = 1; $i <= 12; $i++) {
$months[] = date("t/m/Y l", strtotime(date('Y-m')." -$i months"));
}
print_r($months);
?>
Output:
输出:
Array
(
[0] => 30/11/2018 Monday
[1] => 31/10/2018 Friday
[2] => 30/09/2018 Wednesday
[3] => 31/08/2018 Sunday
[4] => 31/07/2018 Thursday
[5] => 30/06/2018 Tuesday
[6] => 31/05/2018 Saturday
[7] => 30/04/2018 Thursday
[8] => 31/03/2018 Monday
[9] => 28/02/2018 Monday
[10] => 31/01/2018 Friday
[11] => 31/12/2017 Tuesday
)
回答by Dan Streeter
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

