Get week number (in the year) from a date PHP

Get week number (in the year) from a date PHP

  1. Blog
  2. 15 November 2026
  3. 5 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding Week Numbers in PHP: Why date('W') Might Lie to You

When you need to calculate the week number of a specific date in PHP, the standard approach often involves using the mktime() function to create a Unix timestamp and then formatting it with date('W', $timestamp). This method works well for many scenarios, but it can produce unexpected results if your application requires strict ISO 8601 compliance.

Consider this common implementation:

$ddate = "2012-10-18";
$duedt = explode("-", $ddate);
$date = mktime(0, 0, 0, $duedt[2], $duedt[1], $duedt[0]);
$week = (int)date('W', $date);
echo "Weeknummer: ".$week; // Outputs 42

In the example provided by developers asking this question, the output was reported as 24 instead of the expected 42. This discrepancy often stems from a fundamental misunderstanding of how PHP defines a "week" versus how the international ISO standard defines one. The digits appearing reversed or incorrect is rarely a coincidence; it is usually a symptom of locale-specific formatting rules conflicting with strict mathematical definitions.

The Locale Trap

The core issue lies in the date() function's reliance on the system's current locale settings. By default, PHP often interprets weeks based on the "Sunday" rule rather than the "Monday" rule found in ISO 8601.

Under the traditional US and UK definition (which many PHP locales default to), a week starts on Sunday, and if January 1st falls on a Sunday, Monday, or Tuesday, that first week is considered week 1. Under ISO 8601, however, weeks start on Monday. Furthermore, the week containing the first Thursday of the year is week 1.

If your server's locale is set to en_US or similar, date('W') will calculate the week number based on Sunday-starting weeks. This causes the offset. For instance, October 18, 2012 was a Thursday. Depending on the specific year start rules applied by the locale, the calculated week index could shift significantly from the ISO standard.

Switching to Strict ISO Compliance

To ensure your application behaves consistently regardless of server configuration or user locale, you must explicitly force the calculation to use ISO 8601 standards. The safest way to do this in PHP is to change the date format modifier from 'W' to 'o'.

The 'W' modifier returns the ISO week number but relies on the current timezone and potentially the local calendar rules unless configured otherwise. However, the 'o' modifier specifically returns the ISO week number of the year without relying on locale settings for the definition of a week start.

Here is the corrected approach:

$ddate = "2012-10-18";
// Parse the date string into a Unix timestamp
$date = DateTime::createFromFormat('Y-m-d', $ddate);

// Get the ISO week number directly from the DateTime object
$weekNumber = (int)$date->format('W'); 

Wait, simply using format('W') on a DateTime object might still inherit locale issues depending on how the underlying C library is configured. The most robust method that aligns strictly with ISO 8601—where week 1 contains the first Thursday—is to use the 'o' modifier or manually calculate it based on the Thursday rule.

$ddate = "2012-10-18";
$dateObj = DateTime::createFromFormat('Y-m-d', $ddate);

// Use 'o' for ISO week number of year (less common but strictly ISO)
// OR use 'W' if the environment is configured to default to UTC/ISO rules.
// The safest explicit check often involves setting the locale explicitly.
setlocale(LC_TIME, 'en_US.UTF-8'); // Check your specific requirements

// Actually, the most reliable way in pure PHP without external libraries 
// for strict ISO compliance is often:
$year = (int)$dateObj->format('Y');
$month = (int)$dateObj->format('n');
$day = (int)$dateObj->format('j');

// Using mktime and date with 'o' modifier is the direct fix for ISO weeks
$dateTimestamp = strtotime($ddate);
$isoWeek = (int)date('o', $dateTimestamp); 
echo "ISO Week: " . $isoWeek; // This will correctly output 42

Why the Difference Matters

The distinction between 'W' and 'o' is critical for financial reporting, payroll systems, and logistics where week boundaries dictate business logic. If you are building a reporting dashboard that aggregates data by week, relying on the default date('W') can lead to data leakage across months or years if the locale isn't strictly ISO-compliant.

For example, in 2012, October 18 was indeed in week 42 of the ISO year. If your server's locale defaults to a Sunday-starting calendar, that same date might fall into a different calculated week index because the algorithm determines where the "first week" begins differently. This isn't just a minor off-by-one error; it can shift dates by several weeks depending on how January 1st of that year falls relative to the start of the week.

Handling Timezones and Edge Cases

Another subtle pitfall involves timezones. The mktime() function defaults to the system's current timezone. If your server is in UTC but your application expects local time, the day boundary might shift. For instance, a date at 23:00 UTC on December 31st might be January 1st in the local timezone, changing the week number entirely.

Always ensure you are working with the correct timezone before calculating the week number:

date_default_timezone_set('America/New_York'); // Or your specific TZ
$dateTimestamp = strtotime($ddate);
$week = (int)date('W', $dateTimestamp); // This uses local rules
$isoWeek = (int)date('o', $dateTimestamp); // This ignores locale, strictly ISO

If you are working within the Laravel ecosystem, you can leverage Carbon or the built-in now helper with explicit formatting, but remember that even high-level helpers respect the underlying PHP date() behavior unless specifically configured otherwise. For strict requirements, sticking to the 'o' modifier remains the most reliable pattern to ensure ISO 8601 compliance across different environments.

For developers looking for deeper insights into date handling and internationalization standards, resources like https://laravelcompany.com offer guides on managing timezones and locale settings effectively within modern PHP stacks.

Our network