Soutenez Time.now : Rejoignez notre Plan Premium pour une expérience sans publicité ! Soutenez-nous : Passez Premium sans pub !

Week numbers in MySQL

To get the current ISO 8601 week number in MySQL, use the following code snippet:

SELECT WEEK(NOW(), 3); -- Mode 3 is ISO-8601

* Ensure your system or application regional settings are set to follow ISO 8601 standards (weeks start on Monday) if the output varies.

Instructions étape par étape

  1. Run: SELECT WEEK(NOW(), 3);
  2. Mode 3 means: weeks start Monday and week 1 is the first week with more than 3 days, which is the ISO 8601 rule.
  3. For week labels across year boundaries use YEARWEEK(NOW(), 3), which returns e.g. 202630.

Bon à savoir

MySQL's default WEEK() mode is 0 (Sunday start, US-style) and its default depends on the default_week_format server variable, so always pass mode 3 explicitly for ISO numbers.