MySQL WEEK()的哪种模式符合ISO 8601

时间:2012-08-03 03:33:42

标签: mysql iso8601

MySQL的WEEK()函数的哪种模式产生ISO 8601 week of the yearWEEK()的参数2根据此图表设置模式:

+--------------------------------------------------------------------+
| Mode | First day of week | Range | Week 1 is the first week ...    |
|------+-------------------+-------+---------------------------------|
| 0    | Sunday            | 0-53  | with a Sunday in this year      |
|------+-------------------+-------+---------------------------------|
| 1    | Monday            | 0-53  | with more than 3 days this year |
|------+-------------------+-------+---------------------------------|
| 2    | Sunday            | 1-53  | with a Sunday in this year      |
|------+-------------------+-------+---------------------------------|
| 3    | Monday            | 1-53  | with more than 3 days this year |
|------+-------------------+-------+---------------------------------|
| 4    | Sunday            | 0-53  | with more than 3 days this year |
|------+-------------------+-------+---------------------------------|
| 5    | Monday            | 0-53  | with a Monday in this year      |
|------+-------------------+-------+---------------------------------|
| 6    | Sunday            | 1-53  | with more than 3 days this year |
|------+-------------------+-------+---------------------------------|
| 7    | Monday            | 1-53  | with a Monday in this year      |
+--------------------------------------------------------------------+

这些模式中的一种是否会提供一年中的ISO 8601周?

4 个答案:

答案 0 :(得分:64)

在ISO周编号中,星期一是一周的第一天,所以单独将它缩小到奇数模式之一。

Per Wikipedia

  

第01周有相互对应的描述:

     
      
  • 本周第一个星期四(正式的ISO定义),
  •   
  • 1月4日的一周,
  •   
  • 第一周,在开始年份的大部分时间(四次或更多次)和
  •   
  • 从12月29日至1月4日的星期一开始的一周。
  •   

这些描述中的第三个与上表中的“今年超过3天”匹配,所以现在我们将其缩小到1或3。

最后,仍然来自维基百科(重点补充):

  

如果1月1日是星期一,星期二,星期三或星期四,则是01周。如果1月1日是星期五,星期六或星期日,则是上一年的第52周或第53周(没有第00周

因此,范围必须是1-53,而不是0-53。这反过来意味着正确的模式是模式3

答案 1 :(得分:22)

我知道这个问题已经过时了,但是SEO定位很好:) 所以只是为了让答案更加完整 - 当显示年份和周数时,您可以使用:

DATE_FORMAT(your_date_here, "%x-%v")

它将生成“%v”(1-53)的iso周数和“%x”的正确年份数。

答案 2 :(得分:1)

ISO 8601 的答案是模式 3

SELECT week(your_date_column, 3) FROM your_table

答案 3 :(得分:0)

对于法国,您必须输入文件/etc/mysql/my.cnf§[Mysqld]: default_week_format = 3

注意:不适用于YEARWEEK,需要添加模式= 3 SELECT YEARWEEK(CURDATE(),3)

Pour la France vous devez modifier ou ajouter cette ligne dans le paragraphe [Mysqld] du fichier /etc/mysql/my.cnf: default_week_format = 3 Celapermetàlafonction WEEK de retourner les vraiesnumérosdesemainesFrançaise。 注意事项YEARWEEK ilfautintérativementajouterlemodeà3 SELECT YEARWEEK(CURDATE(),3)

相关问题