Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:47:34+00:00 2026-06-05T11:47:34+00:00

I’d like to have the week number and year, for a given date, from

  • 0

I’d like to have the week number and year, for a given date, from MySQL with the following rules:

  • If the date is at the end of the year, but in the first week of the next year, I need to return 1 as the week number.
  • If the date is at the beginning of the year, but in the last week of the previous year, I need to return 52 (or 53) as the week number.

I’ve read the week function in MySQL but I can’t get the result I want.

Date and Time Functions: WEEK(date[,mode])

I am on the french calendar, so I have to begin the week on Monday and week 1 is the first week with more than 3 days this year.

Therefore I can only use options 1 and 3.

When I write the following queries:

  • select week (‘2012-12-31’, 3), the result is 1

  • select week (‘2012-12-31’, 1), the result is 53

When I test on the 1st Jan 2016:

  • select week (‘2016-1-1’, 3), the result is 53

  • select week (‘2016-1-1’, 1), the result is 0

Option 1 can’t be used, because I can’t detect that 2012-12-31 is in the next year.

Option 3 can be used, but I have the add two pieces of logic: if weeknumber = 1 and month = 12, year + 1 and if weekumber = 53 and month = 1 then year – 1

Does someone have a better solution?

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:47:36+00:00Added an answer on June 5, 2026 at 11:47 am

    Ok, I think I get what you’re trying to do now.

    As the documentation says:

    We decided to return 0 instead because we want the function to return
    “the week number in the given year.”

    If you want the week number for the year that the week is in, they suggest using the YEARWEEK() function, which takes the same mode arguments as WEEK():

    If you would prefer the result to be evaluated with respect to the
    year that contains the first day of the week for the given date…
    use the YEARWEEK() function:

     mysql> SELECT YEARWEEK('2000-01-01');
             -> 199952
     mysql> SELECT MID(YEARWEEK('2000-01-01'),5,2);
             -> '52'
    

    So some examples of what you’d use:

    mysql> SELECT MID(YEARWEEK('2012-1-1',3),5,2)
             -> '52'
    mysql> SELECT MID(YEARWEEK('2012-12-31',3),5,2)
             -> '01'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a view passing on information from a database: def serve_article(request, id): served_article
I would like to count the length of a string with PHP. The string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.