PHP的日期时间错误? [英] php datetime bug?

查看:75
本文介绍了PHP的日期时间错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用php的DateTime函数时遇到问题。
今天是12月3日,星期一。

I am having a problem with php's DateTime functions. Today is monday 3 december.

假定以下代码:

$dte = new DateTime(date('Y-m-d H:i:s'));
var_dump($dte->format('Y-W'));
$dte->modify('+4 weeks');
var_dump($dte->format('Y-m-d H:i:s -- Y_W'));
$dte->modify('+1 days');
var_dump($dte->format('Y-m-d H:i:s -- Y_W'));

四个星期后,即12月31日。我怀疑会到达一年的最后一周(52?)。但是我得到的是2012年的第1周,如下面的输出所示。

After four weeks it would be 31st of december. I would suspect to get the last week of the year (52?). But what I get is week 1 of 2012 as you can see in the following output.

string '2012-49' (length=7)
string '2012-12-31 14:48:00 -- 2012_01' (length=30)
string '2013-01-01 14:48:00 -- 2013_01' (length=30)

所以我的问题是,在第一次修改后,我认为我应该得到:

So my problem is that after the first modification I think I should get:

2012-12-31 14:48:00 -- 2012_52

但我却得到

2012-12-31 14:48:00 -- 2012_01

为什么一周不增加年份又回到01,为什么?另一行给我2013_01吗?

So why does the week go back to 01 without incrementing the year, and than why does the other line gives me 2013_01 ?

编辑::
我现在看到前一周是第52周,与leap年有关系吗?
但是又一次,一周如何回到01而又不增加年份?

: I now see that the week before is week 52, anything to do with leap year? But then again, how can the week go back to 01 without incrementing the year?

推荐答案


那么为什么一周不增加年份又回到01,为什么另一行给我2013_01?

So why does the week go back to 01 without incrementing the year, and than why does the other line gives me 2013_01 ?

我认为您显示的是年而不是周年。当您使用星期数时,相关的部分是星期几;简单的年仅与月份和日期有关。

I think you're displaying the "year" instead of the "week-year". When you're using week numbers, it's the week-year that's the relevant part; simple "year" is only relevant with respect to month and day.

编辑:我认为您想要 o 格式说明符,请尝试:

I think you want the o format specifier instead, so try:

var_dump($dte->format('Y-m-d H:i:s -- o_W'));

应该可以在12月31日显示 2013_01 2012年,因为它是在2013年的第1周。

That should show you 2013_01 for December 31st 2012, as it's in week 1 of week-year 2013.

因此,基本上,我认为这不是 DateTime中的错误-这只是对一年中的一周的使用方式的误解。

So basically, I don't think this is a bug in DateTime - it's just a misunderstanding of how "week of year" is meant to be used.

这篇关于PHP的日期时间错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆