在PHP中获取上个月的日期

pg.*_*pg. 79 php date

我想得到上个月的约会.我写了这个:

$prevmonth = date('M Y');
Run Code Online (Sandbox Code Playgroud)

这给了我当前的月/年.我不知道我是否应该使用strtotime,mktime.什么到时间戳?我是否需要在重置后添加一些内容,以便在我网站上的所有时间戳的所有内容中将日期设置为上个月?我正在尝试RTM,但我很难弄清楚这一点.

Ozz*_*ech 195

上个月约会很简单

echo date("Y-n-j", strtotime("first day of previous month"));
echo date("Y-n-j", strtotime("last day of previous month"));
Run Code Online (Sandbox Code Playgroud)

11月3日回归

2014-10-1
2014-10-31
Run Code Online (Sandbox Code Playgroud)

  • 这个不够好,可能会导致代码错误:`$ time = strtotime('2011-03-30 01:01:01'); echo date('r',strtotime(' - 1 month',$ time));`这个将返回Wed,02 Mar 2011 01:01:01 - 不是2月!使用`strtotime('上个月的第一天')来代替 (7认同)

zom*_*bat 29

echo strtotime("-1 month");
Run Code Online (Sandbox Code Playgroud)

这将完全输出上个月的时间戳.之后你不需要重置任何东西.如果您之后想要英文格式,可以使用date()格式化时间戳,即:

echo date("Y-m-d H:i:s",strtotime("-1 month"));
Run Code Online (Sandbox Code Playgroud)

  • 今天是2012年10月31日.日期("Ymd",strtotime(" - 1个月")); 返回2012-10-01.:( (12认同)
  • @toddsler正确.-1个月与写作-30天相同,在一年中的某些日期将跳过1个月或保持在同一个月,因此请谨慎使用此方法. (7认同)
  • 不要使用此解决方案!之前的评论是正确的,您会遇到错误,只会在该月的 31 天(或 3 月 29、30、31)触发 (2认同)

Sch*_*els 19

$prevmonth = date('M Y', strtotime("last month"));
Run Code Online (Sandbox Code Playgroud)

  • 这与日期('M Y',strtotime(" - 1个月"))相同,如果您执行类似日期('M Y',strtotime("2017-07-31上个月")),它将返回2017-07-01,所以要小心!日期('M Y',strtotime('2017-07-31上个月的第一天'))会给你你想要的 (3认同)

Fur*_*ury 10

答案不正确:

$lastMonth = date('M Y', strtotime("-1 month"));
$lastDate = date('Y-m', strtotime('last month'));
Run Code Online (Sandbox Code Playgroud)

原因是当前月份为30天以上,但前一个月为29天,较少$ lastMonth将与当月相同.

例如

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
Run Code Online (Sandbox Code Playgroud)

正确答案是:

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
Run Code Online (Sandbox Code Playgroud)

  • 如果您删除 echo sprintf("%02d",date("m")-1) ,我会投票。日期(“-Y”);=> 02-2016。如果我们在一月份,它会回答今年的第 0 个月,而不是去年的第 12 个月! (2认同)

小智 7

如果你想在上个月获得,那么你可以使用如下

$prevmonth = date('M Y', strtotime('-1 months'));
Run Code Online (Sandbox Code Playgroud)

如果你想获得上个月的同一天,那么你可以像下面一样使用..

$prevmonth = date('M Y d', strtotime('-1 months'));
Run Code Online (Sandbox Code Playgroud)

如果你想获得上个月的最后一个日期,那么你可以使用如下...

$prevmonth = date('M Y t', strtotime('-1 months'));
Run Code Online (Sandbox Code Playgroud)

如果你想获得上个月的第一个日期,那么你可以使用如下...

$prevmonth = date('M Y 1', strtotime('-1 months'));
Run Code Online (Sandbox Code Playgroud)

  • 请注意本月31日!它将给您本月的第一天!往上看。 (3认同)

Jay*_*_69 6

当前几个月短于当前月份时发现这个错误.

echo date("Y-m-d H:i:s",strtotime("-1 month"));
Run Code Online (Sandbox Code Playgroud)

试试3月30日,您将获得2012-03-01而非2012-02 ...

努力寻求更好的解决方案......

  • 这个答案是正确的(-1个月在所有情况下都不会正常工作).在上个月,使用strtotime('上个月'). (3认同)

Vai*_*shu 5

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>
Run Code Online (Sandbox Code Playgroud)

  • 当前日期为2018/03/31时,此答案不正确。细节也像[this](/sf/answers/2541746651/) (3认同)