Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

date("W") returns 52 to first days in a year

Hi Experts!

Why first days of year can be in a week of previous year when using:

echo date("W", strtotime("2011-01-01"));

It returns 52.

Somebody can help me to fix it?

Thanks in advance!
  • 2
2 Solutions
Garry GlendownConsulting and Network/Security SpecialistCommented:
Actually can't fix what's not broken ... depending on the weekday, up to three days of the new year are still counted as belonging to the last week of the previous year ... (for Jan 1st being Friday, Saturday or Sunday)
Hugh McCurdyCommented:
It's not broken.

The issue is that the weeks start on Monday.  http://php.net/manual/en/function.date.php

    January 2011
Su Mo Tu We Th Fr Sa
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

Open in new window

January 1st and 2nd thus belong to 2010 because they are before the first Monday.  52 is correct.
Hugh McCurdyCommented:
I'm not sure what you want as a result.  You could check the month -- date ( "m", $timestamp );  If the month is 1 and the week is 52, you know it's early January.  You could call that "week 0" if you want.  Or if you want to call that week 1, then you just have to add 1 to the result.  But then you will end up with week 53 at the end of the year.

You could do something similar if you want to start the week counter on January 1st.  The math here is a bit more complicated and I can help you with it if you want help with it.  (I'm not going to waste effort if you don't want the effort).  

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now