php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65730 DateTime::format('W') returns wrong week number
Submitted: 2013-09-21 11:04 UTC Modified: 2013-09-21 20:56 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: glavic at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: Irrelevant OS: ALL
Private report: No CVE-ID:
 [2013-09-21 11:04 UTC] glavic at gmail dot com
Description:
------------
DateTime::format('W') returns wrong week number when date is 2013-12-30 or 2013-12-31.

Test script:
---------------
<?php
$dt = new DateTime;
for ($w = 52; $w <= 54; $w++) {
	$dt->setISODate(2013, $w);
	print_r($dt);
	echo $dt->format('Y W') . "\n\n";
}

# see this script running on all php versions http://3v4l.org/9aua5

Expected result:
----------------
DateTime Object
(
    [date] => 2013-12-23 11:02:12
    [timezone_type] => 3
    [timezone] => UTC
)
2013 52

DateTime Object
(
    [date] => 2013-12-30 11:02:12
    [timezone_type] => 3
    [timezone] => UTC
)
2014 01 # or 2013 53, but surely not 2013 01 !!!

DateTime Object
(
    [date] => 2014-01-06 11:02:12
    [timezone_type] => 3
    [timezone] => UTC
)
2014 02



Actual result:
--------------
DateTime Object
(
    [date] => 2013-12-23 11:02:12
    [timezone_type] => 3
    [timezone] => UTC
)
2013 52

DateTime Object
(
    [date] => 2013-12-30 11:02:12
    [timezone_type] => 3
    [timezone] => UTC
)
2013 01

DateTime Object
(
    [date] => 2014-01-06 11:02:12
    [timezone_type] => 3
    [timezone] => UTC
)
2014 02



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-09-21 14:33 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2013-09-21 14:33 UTC] rasmus@php.net
PHP uses the ISO 8601 standard for day, month and week numbers. This is pretty 
clearly documented and if you read ISO 8601 it states that the first week of the 
year is "the week with the year's first Thursday in it" and since ISO 8601 weeks 
start on Mondays, it is quite normal for the first ISO week to contain days from 
a previous year. If they didn't then it would be impossible to consistently start 
weeks on Mondays since Jan.1 is not always a Monday. If you apply the ISO 8601 
definition to the dates in question here you will find PHP is quite correct here.
 [2013-09-21 18:05 UTC] glavic at gmail dot com
@rasmus: I never said nothing about first or last week of the month containing dates from before/after year. You probably agree with statement that the last week of the year 2013 (and first week of the year 2014) is from 2013-12-30 to 2014-01-05? 

I reported this, not because what you said, but because code "echo (new DateTime('2013-12-30'))->format('Y W');" returns "2013 01" which is incorrect, and for me this is bug. This should, like I already said in report, return "2014 01".
Code "echo (new DateTime('2012-12-31'))->format('Y W');" also returns "2013 01". So now we have two identical week number for year 2013 with a year difference between dates ;)
 [2013-09-21 18:18 UTC] rasmus@php.net
2013-12-30 ('Y W');" returns "2013 01" is correct. The 'Y' is the year. The ISO 
8601 Year for 2013-12-30 is obviously 2013. However the ISO 8601 Week number for 
2013-12-30 is also just as obviously 1. So 2013 01 is correct. 

And your next statement makes no sense either:

"So now we have two identical week number for year 2013 with a year difference 
between dates ;)"

We are talking about Dec.30 and 31 in 2013. That is Monday and Tuesday. They 
both fall in ISO week 1 and they are both in 2013. Wednesday Jan.1 2014 also 
falls in ISO week 1 even though it is in 2014. That's how ISO weeks work.

Is your confusion that you think the 'Y' has to be associated with the 'W' 
somehow? The 'Y' refers to the date you are formatting.
 [2013-09-21 18:48 UTC] glavic at gmail dot com
I made a copy/paste typo before, but that doesn't matter now because you said that 'Y' returns the year of the date, which is totally correct, where was my head...

I understand what you are saying; I agree that Dec 30 and 31 fall in week 1; but year 2014, not 2013.

Maybe I should rephrase my question: how do I get correct week year from "echo (new DateTime('2013-12-30'))->format('W ????');" ?
 [2013-09-21 18:51 UTC] glavic at gmail dot com
Ah, I found it, it is 'o' format character.
Sorry for your trouble. Can you delete this failed bug report? ;)
Tnx
 [2013-09-21 20:56 UTC] derick@php.net
Nope, we won't delete it so that we can point other people to it in the future.
 [2013-11-27 21:39 UTC] rodolphe at eveilleau dot fr
Thank you for not delete these comments. I encountered the same situation.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 18:01:58 2014 UTC