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

Posted on 2011-10-11
Last Modified: 2012-06-27
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!
Question by:ZumbaJr
    LVL 17

    Assisted Solution

    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)
    LVL 13

    Expert Comment

    by:Hugh McCurdy
    It's not broken.

    The issue is that the weeks start on Monday.

        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.
    LVL 13

    Accepted Solution

    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).  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now