简体   繁体   English

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

[英]PHP date format issue using strtotime()

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 . 我想知道是否由于-1 month而导致计算不准确。

Is there a way of simply subtracting 1 from m in ...date("Y, m, d", ... ? 有没有简单地减去方法1 ,从m...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 . 我的数据需要格式化为JavaScript Date Object ,其中January是0,Feb是1,依此类推。因此,没有必要专门减去1个月,而实际上是从month整数中减去1 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. 最后,结果字符串不应该早于1个月,而是实际上是相同的日期,使用JS Date Object样式表示。 I believe @vprimachenko's answer below is a good solution. 我相信@vprimachenko在下面的答案是一个很好的解决方案。 I apologize if this wasn't clear in my OP. 如果在我的OP中不清楚,我深表歉意。

Thanks! 谢谢!

you might use 你可能会用

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

strtotime might work in an unexpected way but it is logical 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

Here is one kind of fix http://www.phpreferencebook.com/tips/fixing-strtotime-1-month/ 这是一种修补程序http://www.phpreferencebook.com/tips/fixing-strtotime-1-month/

The calculation isn't inaccurate, per se. 计算本身并不准确。 There is no 2/29/2011. 没有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. 如果将输入更改为2012年3月29日,您会看到它返回2012年2月29日,因为2012年是a年。 The same would happen with using something like 7/31/2011. 使用7/31/2011之类的东西也会发生同样的情况。 June only has 30 days, so July 31 minus one month would be July 1 (because June 31 doesn't exist). 6月只有30天,因此7月31日减去一个月就是7月1日(因为6月31日不存在)。

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. 您可以提取月份,然后减去1,然后重新制作日期,但这将导致尝试制作不存在的日期。

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: 如果确实需要上个月的相应日期,则可能需要按照以下内容对某些内容进行if声明,以使日期回溯到2月的最后一天:

$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. 您还必须考虑2月所没有的3月的其余日子以及leap年,并在30天之后的31天中执行类似的操作。

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

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