简体   繁体   English

在几个月前查找日期,如果当前月份更短,它会错过日期吗?

[英]find date months ago, will it miss dates if current month is shorter?

I am writing a script to query dates "3 months ago". 我正在写一个脚本来查询“3个月前”的日期。 As I am thinking about making sure I do not miss anything, I am using this for comparison: 当我在考虑确保我不会遗漏任何东西时,我将其用于比较:

$date = date("Y-m-d",mktime(0,0,0,date("m")-$months_ago,date("d")));

I am realizing there is a chance I could be missing dates. 我意识到我有可能错过约会。 For example, if I search on 11/30 for dates three months ago, I will get 8/30. 例如,如果我在11月30日搜索三个月前的日期,我会得到8/30。 But the next day is 12/1 and three months prior is 9/1. 但第二天是12/1,三个月前是9/1。 So in this script I have missed 8/31. 所以在这个剧本中我错过了8/31。

I guess the best method is to use days (90 days) instead on months. 我想最好的方法是使用天数(90天)而不是几个月。 Is this the best practice for something like this? 这是这样的最佳做法吗?

Go directly with strtotime('-3 month'); 直接用strtotime('-3 month'); you can also give negatives like -3 on the month param of the mktime and it will work like charm (better the second solution). 你也可以在mktime的月份参数上给出-3的负数,它会像魅力一样工作(更好的是第二种解决方案)。 No, it wont skip days - if "now" is 2011-06-17 it would return timestamp equivalent to 2011-03-17. 不,它不会跳过几天 - 如果“现在”是2011-06-17它将返回相当于2011-03-17的时间戳。

Edit: Well, it might actually be true that you can miss days (I haven't checked your statement) but after all your unit of measurement of time is months, not days. 编辑:嗯,实际上你可能错过了几天(我没有检查你的陈述),但毕竟你的时间单位是几个月,而不是几天。 What I'm saying is that in the Gregorian calendar month isn't constant amount of time - it could be 28, 29, 30 or 31 days. 我所说的是,在阳历月里不是恒定的时间 - 可能是28天,29天,30天或31天。

Let's say you want to calculate months for a paid subscription period. 假设您想计算付费订阅期的月数。 If the user pays one month on 2011-02-15, when would his subscription expire? 如果用户在2011-02-15支付一个月,他的订阅何时到期? I would guess 2011-03-15, even though there are just 28 days between those two dates And if he pays for subscription on 2011-03-15, he would get full 31 days till 2011-04-15 and this seems perfectly fair to me as the subscription is "one month", which just happens to be different amount of days through the year. 我猜想2011-03-15,即使这两个日期之间只有28天如果他支付2011-03-15的订阅费用,他将在2011-04-15之前满31天,这似乎完全公平对我来说,订阅是“一个月”,这恰好是一年中不同的天数。

If, in your case you don't want to get "3 months ago" but want to get constant amount of time that relatively represents "3 months", then you can use the medium month length - 88.59 days, or 88 days 14 hours and 10 minutes. 如果,在你的情况下,你不想“3个月前”,但想要获得相对代表“3个月”的恒定时间,那么你可以使用中等月份长度 - 88.59天,或88天14小时10分钟 That represented with code would be: 用代码表示的将是:

strtotime('-88 days -14 hours -10 minutes');

$when = strtotime('-3 months');

If you just need the month/year and don't want to have to calculate days: 如果您只需要月/年并且不想计算天数:

$m = 5;  // how many months ago, for example

$now = time();
$cm = date("m",$now); // current month
$yr = date("Y",$now) - intval((12 + $m - $cm)/12);
$month = (($cm + 11 - ($m % 12) ) % 12) + 1;

echo "$m months ago: $month  yr: $yr\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM