Skip to content

Date: Pad the ISO week number to two digits, matching PHP - #81299

Open
konnen916 wants to merge 1 commit into
WordPress:trunkfrom
konnen916:fix/date-iso-week-padding
Open

Date: Pad the ISO week number to two digits, matching PHP#81299
konnen916 wants to merge 1 commit into
WordPress:trunkfrom
konnen916:fix/date-iso-week-padding

Conversation

@konnen916

Copy link
Copy Markdown

What?

Closes #81298

format returns an unpadded ISO week number where PHP pads it to two digits. This maps W onto Moment's padded WW.

Why?

The formatMap entry is W: 'W'. Moment's W is the ISO week without padding, PHP's date( 'W' ) pads to two digits, so weeks 1 to 9 come back a character short:

format( 'W', '2024-01-15' ); // '3', PHP gives '03'

Weeks 10 and up are already two characters and agree, which is why this has been quiet. It fails by returning a plausible string rather than raising anything, so a wrong value propagates instead of surfacing.

Verified against PHP 8.4.24:

Date PHP before after
2024-01-15 03 3 03
2024-01-01 01 1 01
2020-02-29 09 9 09
2024-03-05 10 10 10
2026-12-31 53 53 53

I checked the other format characters with custom handling in that map against PHP over the same dates, including a leap day and both ISO year boundaries. z, t, L, o, N, w and S all already agree. W was the only divergence.

How?

One character: W: 'W' becomes W: 'WW'.

Five test cases are added. The existing test for W uses 18 June 2019, week 25, which is two digits and therefore passes either way. The new cases use single digit weeks, and three of the five fail without the source change.

Testing Instructions

  1. Open a post or page.
  2. Open the console.
  3. Run wp.date.format( 'W', '2024-01-15' ).
  4. On trunk it returns '3'. With this branch it returns '03', matching date( 'W' ) in PHP.

Or run the package tests:

npm run test:unit -- packages/date

115 pass. Reverting only the change to packages/date/src/index.ts and rerunning drops three of the new cases to failing, which is the check that they are testing the right thing.

Testing Instructions for Keyboard

Not applicable, this is a package-level change with no UI.

Screenshots or screencast

Not applicable.

The `W` format character maps onto Moment's `W`, which is unpadded, while
PHP's `date( 'W' )` pads to two digits. So any week before the tenth of
the year came back a character short: `3` where PHP gives `03`.

Verified against PHP 8.4:

    date        PHP   before   after
    2024-01-15   03      3      03
    2024-01-01   01      1      01
    2020-02-29   09      9      09
    2024-03-05   10     10      10
    2026-12-31   53     53      53

The existing test for `W` uses 18 June 2019, which falls in week 25. Two
digits either way, so it passed regardless of the padding. The added
cases use single digit weeks and fail without this change.
@konnen916
konnen916 force-pushed the fix/date-iso-week-padding branch from 9d84e39 to 8df95fa Compare August 8, 2026 15:35
@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @konnen916.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: konnen916.


To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions Bot added the [Package] Date /packages/date label Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Date /packages/date

Projects

None yet

Development

Successfully merging this pull request may close these issues.

format() returns an unpadded ISO week number for W, where PHP pads to two digits

1 participant