PHP date() 和 strtotime() 返回错误的月份 31 日

发布于 2025-01-01 07:06:10 字数 593 浏览 0 评论 0原文

我使用 date()strtotime() 函数在下拉列表中显示接下来的 3 个月。

PHP 代码:

   echo date("m/Y",strtotime("+0 months")); 
   echo date("m/Y",strtotime("+1 months"));
   echo date("m/Y",strtotime("+2 months")); 

但是,如果脚本在服务器日期为 30 日或 31 日时运行,则下个月(二月)将显示为三月。 即上面的脚本应该返回

01/2012
02/2012
03/2012

但是,它实际上显示

01/2012
03/2012
03/2012

的是因为二月没有 30 日或 31 日,所以脚本将“31/02”翻译为“01/03”。

我读过php.net上的strtotime()页面,已经提出了这个问题,但还没有任何有用的解决方案。那么有人可以帮我找到一个简单的方法来解决这个问题吗?提前致谢!

I'm using date()and strtotime() functions to display the next 3 months in a dropdown list.

PHP Code:

   echo date("m/Y",strtotime("+0 months")); 
   echo date("m/Y",strtotime("+1 months"));
   echo date("m/Y",strtotime("+2 months")); 

However, if the script is running when the server date is on 30th or 31st, the next month, which is Feburary, will be displayed as March instead.
i.e. the script above is supposed to return

01/2012
02/2012
03/2012

But, instead of that, it actually displays

01/2012
03/2012
03/2012

that is because Feburary doesn't have 30th or 31st, so the script translates "31/02" into "01/03".

I have read the strtotime() page on php.net, this problem has been raised, but there has not been any useful solutions. So can anyone please help me to find a simple way to solve this problem? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乜一 2025-01-08 07:06:10

正如文档中所述,您应该将当月第一天的日期作为第二个参数传递给 strtotime() 函数:

$base = strtotime(date('Y-m',time()) . '-01 00:00:01');
echo date('m/Y',strtotime('+0 month', $base));
echo date('m/Y',strtotime('+1 month', $base));
echo date('m/Y',strtotime('+2 month', $base));

查看它是否有效:http://ideone.com/eXis9

01/2012

02/2012

03/2012

As mentioned within the documentation, you should pass the date for the first day of the current month as the second parameter to the strtotime() function:

$base = strtotime(date('Y-m',time()) . '-01 00:00:01');
echo date('m/Y',strtotime('+0 month', $base));
echo date('m/Y',strtotime('+1 month', $base));
echo date('m/Y',strtotime('+2 month', $base));

See that it works: http://ideone.com/eXis9

01/2012

02/2012

03/2012

錯遇了你 2025-01-08 07:06:10

尝试在 strtotime 中使用“第一天”,如下所示:

strtotime("first day of +1 month");

这将修复日期(如果今天是 1 月 30 日),例如 02-30(3 月 2 日产量),方法是将其转换为 02-01(2 月 1 日),然后为您提供正确的月份。它比其他方法更简洁,也更容易记住。

Try using "first day of" in your strtotime, like this:

strtotime("first day of +1 month");

This will fix dates ( in the event today was Jan 30th ) such as 02-30 (Yields march 2nd) by converting it to 02-01 (Feb 1st) which then gives you the correct month. It's a little cleaner than other methods, and easier to remember.

樱娆 2025-01-08 07:06:10
echo date('m/Y', strtotime(date('Y-m') . '-01 +2 months'));

只需将其硬编码为该月的第一天即可。

echo date('m/Y', strtotime(date('Y-m') . '-01 +2 months'));

Just hard-code it to be the first of the month.

Spring初心 2025-01-08 07:06:10

不要使用 strtotime() 来按月获取偏移日期。它仅在 PHP 5.3+ 中正常工作。
解决此类问题的最佳方法是使用 mktime()
下面是示例代码:

function getOffsetByMonths($nMonths, $nNow = 0) {
    if ($nNow)
        return mktime(0, 0, 0, date('n', $nNow)+ $nMonths, 1, date('Y', $nNow));
    else
        return mktime(0, 0, 0, date('n')+ $nMonths);
}
$nNow = mktime(0, 0, 0, 1, 31, 2013);
echo "Now: ". date("Y-m-d", $nNow).
"<br>(Now - 1 month): ". date("Y-m", getOffsetByMonths(-1, $nNow)). "-xx".
"<br>(Now - 2 month): ". date("Y-m", getOffsetByMonths(-2, $nNow)). "-xx".
"<br>(Now - 3 month): ". date("Y-m", getOffsetByMonths(-3, $nNow)). "-xx";

Don't use strtotime() for getting offset date by month(s). It works properly only in PHP 5.3+.
The best way to solve such problem is using mktime().
Below is a sample code:

function getOffsetByMonths($nMonths, $nNow = 0) {
    if ($nNow)
        return mktime(0, 0, 0, date('n', $nNow)+ $nMonths, 1, date('Y', $nNow));
    else
        return mktime(0, 0, 0, date('n')+ $nMonths);
}
$nNow = mktime(0, 0, 0, 1, 31, 2013);
echo "Now: ". date("Y-m-d", $nNow).
"<br>(Now - 1 month): ". date("Y-m", getOffsetByMonths(-1, $nNow)). "-xx".
"<br>(Now - 2 month): ". date("Y-m", getOffsetByMonths(-2, $nNow)). "-xx".
"<br>(Now - 3 month): ". date("Y-m", getOffsetByMonths(-3, $nNow)). "-xx";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文