php 如何使用 strtotime 和日期获取相对于今天的上一月和一年?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5489502/
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
How to get previous month and year relative to today, using strtotime and date?
提问by mr.b
I need to get previous month and year, relative to current date.
我需要获得相对于当前日期的前一个月和一年。
However, see following example.
但是,请参见以下示例。
// Today is 2011-03-30
echo date('Y-m-d', strtotime('last month'));
// Output:
2011-03-02
This behavior is understandable (to a certain point), due to different number of days in february and march, and code in example above is what I need, but works only 100% correctly for between 1st and 28th of each month.
这种行为是可以理解的(在某种程度上),因为二月和三月的天数不同,上面示例中的代码是我需要的,但在每个月的 1 日和 28 日之间只能 100% 正确地工作。
So, how to get last month AND year (think of date("Y-m")
) in the most elegant manner as possible, which works for every day of the year? Optimal solution will be based on strtotime
argument parsing.
那么,如何以date("Y-m")
最优雅的方式获得上个月和一年(想想),这适用于一年中的每一天?最佳解决方案将基于strtotime
参数解析。
Update. To clarify requirements a bit.
更新。稍微澄清一下需求。
I have a piece of code that gets some statistics of last couple of months, but I first show stats from last month, and then load other months when needed. That's intended purpose. So, during THIS month, I want to find out which month-year should I pull in order to load PREVIOUS month stats.
我有一段代码可以获取过去几个月的一些统计数据,但我首先显示上个月的统计数据,然后在需要时加载其他月份。这就是预期目的。所以,在这个月期间,我想知道我应该拉哪个月份来加载以前的月份统计数据。
I also have a code that is timezone-aware (not really important right now), and that accepts strtotime
-compatible string as input (to initialize internal date), and then allows date/time to be adjusted, also using strtotime
-compatible strings.
我还有一个时区感知代码(现在不是很重要),它接受strtotime
-compatible 字符串作为输入(初始化内部日期),然后允许调整日期/时间,也使用strtotime
-compatible 字符串。
I know it can be done with few conditionals and basic math, but that's really messy, compared to this, for example (if it worked correctly, of course):
我知道它可以用很少的条件和基本数学来完成,但与此相比,这真的很混乱,例如(如果它工作正常,当然):
echo tz::date('last month')->format('Y-d')
So, I ONLY need previous month and year, in a strtotime
-compatible fashion.
所以,我只需要前一个月和一年,以strtotime
兼容的方式。
Answer (thanks, @dnagirl):
回答(谢谢,@dnagirl):
// Today is 2011-03-30
echo date('Y-m-d', strtotime('first day of last month')); // Output: 2011-02-01
采纳答案by dnagirl
Have a look at the DateTime
class. It should do the calculations correctly and the date formats are compatible with strttotime
. Something like:
看看DateTime
班级。它应该正确地进行计算并且日期格式与strttotime
. 就像是:
$datestring='2011-03-30 first day of last month';
$dt=date_create($datestring);
echo $dt->format('Y-m'); //2011-02
回答by ITroubs
if the day itself doesn't matter do this:
如果这一天本身无关紧要,请执行以下操作:
echo date('Y-m-d', strtotime(date('Y-m')." -1 month"));
回答by SeanDowney
I found an answer as I had the same issue today which is a 31st. It's not a bug in phpas some would suggest, but is the expected functionality (in some since). According to this postwhat strtotime actually does is set the month back by one and does not modify the number of days. So in the event of today, May 31st, it's looking for April-31st which is an invalid date. So it then takes April 30 an then adds 1 day past it and yields May 1st.
我找到了一个答案,因为我今天遇到了同样的问题,这是第 31 次。正如某些人所建议的那样,这不是php 中的错误,而是预期的功能(从那时起)。根据这篇文章, strtotime 实际所做的是将月份设置回 1 并且不修改天数。因此,在今天 5 月 31 日的情况下,它正在寻找无效日期的 4 月-31 日。所以它需要 4 月 30 日,然后在它之后增加 1 天并产生 5 月 1 日。
In your example 2011-03-30, it would go back one month to February 30th, which is invalid since February only has 28 days. It then takes difference of those days (30-28 = 2) and then moves two days past February 28th which is March 2nd.
在您的示例 2011-03-30 中,它将返回一个月到 2 月 30 日,这是无效的,因为 2 月只有 28 天。然后取那些天的差值 (30-28 = 2),然后在 2 月 28 日(即 3 月 2 日)之后移动两天。
As others have pointed out, the best way to get "last month" is to add in either "first day of" or "last day of" using either strtotime or the DateTime object:
正如其他人指出的那样,获得“上个月”的最佳方法是使用 strtotime 或 DateTime 对象添加“第一天”或“最后一天”:
// Today being 2012-05-31
//All the following return 2012-04-30
echo date('Y-m-d', strtotime("last day of -1 month"));
echo date('Y-m-d', strtotime("last day of last month"));
echo date_create("last day of -1 month")->format('Y-m-d');
// All the following return 2012-04-01
echo date('Y-m-d', strtotime("first day of -1 month"));
echo date('Y-m-d', strtotime("first day of last month"));
echo date_create("first day of -1 month")->format('Y-m-d');
So using these it's possible to create a date range if your making a query etc.
因此,如果您进行查询等,使用这些可以创建日期范围。
回答by Timo Huovinen
If you want the previous year and month relative to a specific date and have DateTime available then you can do this:
如果您想要相对于特定日期的前一年和一个月,并且有可用的 DateTime,那么您可以执行以下操作:
$d = new DateTime('2013-01-01', new DateTimeZone('UTC'));
$d->modify('first day of previous month');
$year = $d->format('Y'); //2012
$month = $d->format('m'); //12
回答by Marek
date('Y-m', strtotime('first day of last month'));
回答by dieend
strtotime
have second timestamp
parameter that make the first parameter relative to second parameter. So you can do this:
strtotime
有第二个timestamp
参数,使第一个参数相对于第二个参数。所以你可以这样做:
date('Y-m', strtotime('-1 month', time()))
回答by Naftali aka Neal
if i understand the question correctly you just want last month and the year it is in:
如果我正确理解了这个问题,你只想要上个月和它所在的年份:
<?php
$month = date('m');
$year = date('Y');
$last_month = $month-1%12;
echo ($last_month==0?($year-1):$year)."-".($last_month==0?'12':$last_month);
?>
Here is the example: http://codepad.org/c99nVKG8
这是示例:http: //codepad.org/c99nVKG8
回答by dqhendricks
ehh, its not a bug as one person mentioned. that is the expected behavior as the number of days in a month is often different. The easiest way to get the previous month using strtotime would probably be to use -1 month from the first of this month.
呃,它不是一个人提到的错误。这是预期的行为,因为一个月中的天数通常不同。使用 strtotime 获取上个月的最简单方法可能是从本月的第一天开始使用 -1 个月。
$date_string = date('Y-m', strtotime('-1 month', strtotime(date('Y-m-01'))));
回答by Varshaan
date("m-Y", strtotime("-1 months"));
would solve this
会解决这个问题
回答by Zach Rattner
I think you've found a bug in the strtotime function. Whenever I have to work around this, I always find myself doing math on the month/year values. Try something like this:
我认为您在 strtotime 函数中发现了一个错误。每当我不得不解决这个问题时,我总是发现自己在计算月/年值。尝试这样的事情:
$LastMonth = (date('n') - 1) % 12;
$Year = date('Y') - !$LastMonth;