Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating week numbers in PHP using Year, is it a bug?

Tags:

php

That's strange, I don't know if it's a bug, but please, someone try it.

Do that on PHP

echo date("Y-W",strtotime("2014W05 -1 weeks"));

The result will be 2014-04, right? YEAR-WEEKNUMBER. No problem at all. But now, try this:

echo date("Y-W",strtotime("2014W02 -1 weeks"));

Why it's 2013-01, why 2013? What's wrong? It should be 2014-01, right?

What I'm doing wrong? Is it a bug?

My PHP version: 5.4.22

like image 685
saulob Avatar asked Feb 17 '14 23:02

saulob


1 Answers

Y is year from the date
o is ISO-8601 year number

So if you do it like

echo date("o-W",strtotime("2014W05 -1 weeks"))."<br/>";

echo date("o-W",strtotime("2014W02 -1 weeks"));

You will get

2014-04
2014-01

Edit By Jasper

Because a date is a moment rather than a week, you request a week and get the first moment of that week. As per the standards, the week number is based on which year the Thursday is in. As such, the week number is 1, but the date is still in 2013.

like image 165
Hamza Avatar answered Oct 17 '22 10:10

Hamza