使用 strtotime() 的 PHP 日期格式问题

发布于 2024-10-27 02:15:18 字数 748 浏览 1 评论 0原文

我用来

$jsdate = date("Y, m, d", strtotime('-1 month', (strtotime($date))));

转换我的日期

2011-03-28
to
2011, 02, 28

问题是这会产生不可预测的结果。例如今天我得到

2011-03-28
converted to
2011, 02, 28  // OK

 AND

2011-03-29
to
2011, 03, 01 // not OK!

有人知道这里出了什么问题吗?不知道是不是因为-1个月导致计算不准确。

一种方法可以简单地从 ...date("Y, m, d", ... 中的 m 中减去 1

有没有 信息:

我的数据需要格式化为 JavaScript Date Object,其中一月为 0,二月为 1 等。因此不需要专门减去 1 个月,但实际上从月份整数中减去1,最后得到的字符串不应该是早于1个月的,而是实际上相同的日期,使用JS Date Object样式表示。我相信下面的@vprimachenko 的答案是一个很好的解决方案,如果我的操作中没有明确这一点,我深表歉意

I am using

$jsdate = date("Y, m, d", strtotime('-1 month', (strtotime($date))));

to convert my dates from

2011-03-28
to
2011, 02, 28

Problem is this is producing unpredictable results. For example today I got

2011-03-28
converted to
2011, 02, 28  // OK

 AND

2011-03-29
to
2011, 03, 01 // not OK!

Does anyone know what's wrong here? I wonder if the calculation is inaccurate because of the -1 month.

Is there a way of simply subtracting 1 from m in ...date("Y, m, d", ...?

MORE INFO:

My data needs to be formatted as JavaScript Date Object in which January is 0, Feb is 1, etc. Therefore there is not a need to specifically subtract 1 month but actually subtract 1 from the month integer. At the end, the resulting string is not supposed to be 1 month earlier, but actually the same date, represented using JS Date Object style. I believe @vprimachenko's answer below is a good solution. I apologize if this wasn't clear in my OP.

Thanks!

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

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

发布评论

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

评论(3

魂ガ小子 2024-11-03 02:15:18

你可能会使用

$datee = explode('-',$date);
if($datee[1]-- < 0) {
    $datee[1]=12;
    $datee[0]--;
}
$jsdate = implode(', ',$datee);

you might use

$datee = explode('-',$date);
if($datee[1]-- < 0) {
    $datee[1]=12;
    $datee[0]--;
}
$jsdate = implode(', ',$datee);
°如果伤别离去 2024-11-03 02:15:18

计算本身并不准确。没有 2/29/2011。如果将输入更改为 3/29/2012,您将看到它返回 2/29/2012,因为 2012 年是闰年。使用类似 7/31/2011 之类的内容也会发生同样的情况。 6 月只有 30 天,因此 7 月 31 日减去一个月就是 7 月 1 日(因为 6 月 31 日不存在)。

您可以提取月份,减去 1,然后重新创建日期,但这将导致尝试创建不存在的日期。

如果您确实需要上个月的相应日期,您可能需要执行类似于以下内容的 if 语句,以使该日期回滚到二月的最后一天:

$jsdate = date("Y, m, d", strtotime('-1 month', (strtotime($date))));
if($month == '3') {
   $jsdate = date("Y, m, d", strtotime('-1 day', (strtotime($jsdate))));
}

您还必须考虑对于 3 月中 2 月没有的剩余天数以及闰年,并对 30 天月份之后的 31 天月份执行类似的操作。

The calculation isn't inaccurate, per se. There is no 2/29/2011. If you change your input to 3/29/2012, you'll see that it returns 2/29/2012, because 2012 is a leap year. The same would happen with using something like 7/31/2011. June only has 30 days, so July 31 minus one month would be July 1 (because June 31 doesn't exist).

You could just extract the month, subtract 1, and remake the date, but that will result in attempting to make dates that don't exist.

If you really need the corresponding day of the prior month, you'll probably need to do an if statement of something along the lines of the following to make the day roll back to the last day of February:

$jsdate = date("Y, m, d", strtotime('-1 month', (strtotime($date))));
if($month == '3') {
   $jsdate = date("Y, m, d", strtotime('-1 day', (strtotime($jsdate))));
}

You'll also have to account for the rest of the days in March that February doesn't have, as well as leap years, and do something similar for 31-day months that follow 30-day months.

窝囊感情。 2024-11-03 02:15:18

strtotime 可能会以意想不到的方式工作,但它是合乎逻辑的

strtotime('-1 months',strtotime('2011-03-29')  // is 2011-02-29
date('Y-m-d','2011-02-29'); //gets converted to the next real date

这是一种修复方法
http://www.phpreferencebook.com/tips/fixing-strtotime-1-月/

strtotime might work in an unexpected way but it is logical

strtotime('-1 months',strtotime('2011-03-29')  // is 2011-02-29
date('Y-m-d','2011-02-29'); //gets converted to the next real date

Here is one kind of fix
http://www.phpreferencebook.com/tips/fixing-strtotime-1-month/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文