php 在php中获取上个月的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1889758/
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
Getting last month's date in php
提问by pg.
I want to get last month's date. I wrote this out:
我想得到上个月的日期。我写了这个:
$prevmonth = date('M Y');
Which gives me the current month/year. I can't tell if I should use strtotime, mktime. Something to the timestamp? Do I need to add something afterwards to reset so that the date isn't set to last month throughout everything for all timestamps on my site? I'm trying to RTM but it's hard for me to figure this out.
这给了我当前的月份/年份。我不知道我是否应该使用strtotime, mktime。时间戳有什么问题?我是否需要在之后添加一些内容来重置,以便我网站上所有时间戳的所有内容的日期都不会设置为上个月?我正在尝试 RTM,但我很难弄清楚这一点。
回答by OzzyCzech
It's simple to get last month date
获取上个月日期很简单
echo date("Y-n-j", strtotime("first day of previous month"));
echo date("Y-n-j", strtotime("last day of previous month"));
at November 3 returns
在 11 月 3 日返回
2014-10-1
2014-10-31
回答by zombat
echo strtotime("-1 month");
That will output the timestamp for last month exactly. You don't need to reset anything afterwards. If you want it in an English format after that, you can use date()to format the timestamp, ie:
这将准确输出上个月的时间戳。之后您无需重置任何内容。如果之后你想要英文格式,你可以使用date()来格式化时间戳,即:
echo date("Y-m-d H:i:s",strtotime("-1 month"));
回答by Scharrels
$prevmonth = date('M Y', strtotime("last month"));
回答by Fury
Incorrect answers are:
不正确的答案是:
$lastMonth = date('M Y', strtotime("-1 month"));
$lastDate = date('Y-m', strtotime('last month'));
The reason is if current month is 30+ days but previous month is 29 and less $lastMonth will be the same as current month.
原因是如果当前月份是 30 天以上,而上个月是 29 天,那么减去 $lastMonth 将与当前月份相同。
e.g.
例如
If $currentMonth = '30/03/2016';
echo $lastMonth = date('m-Y', strtotime("-1 month")); => 03-2016
echo $lastDate = date('Y-m', strtotime('last month')); => 2016-03
The correct answer will be:
正确答案将是:
echo date("m-Y", strtotime("first day of previous month")); => 02-2016
echo sprintf("%02d",date("m")-1) . date("-Y"); => 02-2016
echo date("m-Y",mktime(0,0,0,date("m")-1,1,date("Y"))); => 02-2016
回答by Md. Maruf Hossain
if you want to get just previous month, then you can use as like following
如果您只想获得上个月,那么您可以使用如下
$prevmonth = date('M Y', strtotime('-1 months'));
if you want to get same days of previous month, Then you can use as like following ..
如果你想获得上个月的同一天,那么你可以像下面这样使用..
$prevmonth = date('M Y d', strtotime('-1 months'));
if you want to get last date of previous month , Then you can use as like following ...
如果您想获得上个月的最后日期,那么您可以使用如下...
$prevmonth = date('M Y t', strtotime('-1 months'));
if you want to get first date of previous month , Then you can use as like following ...
如果您想获得上个月的第一个日期,那么您可以使用如下...
$prevmonth = date('M Y 1', strtotime('-1 months'));
回答by Vaishu
echo date('Y',strtotime("-1 year")); //last year<br>
echo date('d',strtotime("-1 day")); //last day<br>
echo date('m',strtotime("-1 month")); //last month<br>
回答by Jay_69
Found this one wrong when the previous months is shorter than current.
当前几个月比当前时间短时,发现这个错误。
echo date("Y-m-d H:i:s",strtotime("-1 month"));
Try on March the 30th and you will get 2012-03-01 instead of 2012-02...
在 3 月 30 日尝试,您将获得 2012-03-01 而不是 2012-02...
Working out on better solution...
正在寻找更好的解决方案...
回答by Jay_69
public function getLastMonth() {
$now = new DateTime();
$lastMonth = $now->sub(new DateInterval('P1M'));
return $lastMonth->format('Ym');
}
回答by bloody numen
$lastMonth = date('M Y', strtotime("-1 month"));
var_dump($lastMonth);
$lastMonth = date('M Y', mktime(0, 0, 0, date('m') - 1, 1, date('Y')));
var_dump($lastMonth);
回答by abdul
Use this short code to get previous month for any given date:
使用此短代码获取任何给定日期的上个月:
$tgl = '25 january 2012';
$prevmonth = date("M Y",mktime(0,0,0,date("m", strtotime($tgl))-1,1,date("Y", strtotime($tgl))));
echo $prevmonth;
The result is December 2011. Works on a month with shorter day with previous month.
结果是 2011 年 12 月。与上个月相比,在一天较短的月份工作。

