How do I set first day of the week to Monday when using Week(Date) in PHP/MySQL?

前端 未结 2 1755
自闭症患者
自闭症患者 2021-01-22 19:38

I am using the following code to build a Google Chart, to pull in all of entries in a MySQL table corresponding to the Week Numbers in a year. At the moment the Week Numbers st

相关标签:
2条回答
  • 2021-01-22 19:46
    WEEK(date[mode]); 
    
    date = a date value.    
    mode = An integer indicating the starting of the week. 
    

    The default arugment is 0 which is sunday, setting this to 1 will be monday, 2 tuesday and so on.

    week(date,1);
    
    0 讨论(0)
  • 2021-01-22 20:05

    For it to work in your code, you should be able to change the query to:

    "SELECT Sum(Due) FROM patient_sessions WHERE Type='Session' AND Week(Date,1)=$i"
    

    However, I think I could go one step further and use the GROUP BY to make one sql call, and then iterate over the results;

    "SELECT Week(Date,1), SUM(DUE) FROM patient_sessions WHERE Type='Session' GROUP BY Week(Date,1)"
    

    Alternatively, you might need to use WEEK(Date,2) depending on how you want to handle weeks that span between years.

    0 讨论(0)
提交回复
热议问题