Skip to content

Fix: Date block renders with double UTC offset when date is manually set - #81153

Merged
mikachan merged 4 commits into
WordPress:trunkfrom
sanjeev00733:fix/post-date-timezone-naive-double-offset
Nov 19, 2026
Merged

mikachan merged 4 commits into
WordPress:trunkfrom
sanjeev00733:fix/post-date-timezone-naive-double-offset

Conversation

@sanjeev00733

@sanjeev00733 sanjeev00733 commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Closes #81084

Fixes the core/post-date block displaying the wrong time on the front end when a date is manually set via the date picker. On sites with a non-UTC timezone, the displayed time was shifted by the site's UTC offset — as if it were applied twice.

Example (UTC+5:30):

  • Editor: Date block set to 30 July 2026, 4:30 AM
  • Front end (before fix): 30 July 2026, 10:00 AM ❌
  • Front end (after fix): 30 July 2026, 4:30 AM ✅

Root cause

When a date is manually picked, DateTimePicker emits a timezone-naive string such as "2026-07-30T04:30:00" with no UTC offset or Z suffix. The render function passed this directly to strtotime():

$post_timestamp = strtotime( $unformatted_date );

WordPress forces PHP's default timezone to UTC, so strtotime() treats the string as UTC. wp_date() then converts that already-wrong UTC timestamp into the site's local timezone — applying the site offset a second time.

Fix

Replace strtotime() with date_create_immutable() paired with wp_timezone(), which explicitly interprets the timezone-naive string as site-local time from the start:

$date_obj       = date_create_immutable( $unformatted_date, wp_timezone() );
$post_timestamp = $date_obj ? $date_obj->getTimestamp() : false;

If the string already carries a timezone indicator (e.g. a Z or +HH:MM suffix, as produced by the "Now" path), DateTimeImmutable ignores the $timezone argument — so this is backward-compatible for both code paths.

This is the same principle applied in #71430, which fixed the related bug in the fallback/legacy render path (isset() === false). This PR fixes the remaining broken path: when a custom datetime is explicitly set (isset() === true).

How has this been tested?

  • Manually verified on a local WordPress installation with site timezone set to UTC+5:30 (Asia/Kolkata):
    • Inserted a Date block and manually picked 30 July 2026, 4:30 AM
    • Before fix: front end displayed 10:00 AM (offset applied twice)
    • After fix: front end displays 4:30 AM (correct)
    • Confirmed the "Now" toolbar shortcut continues to render correctly
  • PHP unit regression test added to phpunit/blocks/render-block-core-post-date.php
test-date-bug.mov

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Required label: Any label starting with [Type].
  • Labels found: .

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

@github-actions github-actions Bot added [Package] Block library /packages/block-library First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository labels Aug 4, 2026
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @sanjeev00733! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

When a date is manually picked via the date picker, DateTimePicker emits
a timezone-naive string (e.g. "2026-07-30T04:30:00"). The render function
passed this to strtotime(), which assumes UTC because WordPress forces
PHP's default timezone to UTC. wp_date() then applied the site's offset
on top — shifting the displayed time by the full UTC offset a second time.

Fix by replacing strtotime() with date_create_immutable() + wp_timezone(),
which explicitly interprets the string as site-local time. If the string
already carries a timezone indicator (as in the "Now" path), DateTimeImmutable
ignores the $timezone argument — so this is safe for both code paths.

Adds a regression test that sets the site timezone to Asia/Kolkata (UTC+5:30)
and asserts the manually-set time renders correctly without double offset.

Fixes WordPress#81084
@sanjeev00733
sanjeev00733 force-pushed the fix/post-date-timezone-naive-double-offset branch from f968c17 to 6c2cd67 Compare August 4, 2026 13:49
@mikachan mikachan added the [Type] Bug An existing feature does not function as intended label Oct 30, 2026

@mikachan mikachan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sanjeev00733, and welcome! 👋

I think the fix is right. A timezone-naive datetime from the picker should be read in the site's timezone. The REST API treats a post's date field the same way. I checked that strings which already carry an offset still produce the same timestamp as before.

I've left a few inline comments. The main one is that the first test assertion would also pass without the fix.

@github-actions

Copy link
Copy Markdown

🤖 PR meta 🤖

🎉 Props

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

Co-authored-by: sanjeev00733 <infotuts@git.wordpress.org>
Co-authored-by: mikachan <mikachan@git.wordpress.org>

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

sanjeev00733 and others added 2 commits November 19, 2026 14:25
- Assert on the rendered text (>04:30</time>) so the test catches the
  regression. The previous '04:30' substring also matched the datetime
  attribute, so it passed even without the fix.
- Replace the four-line // comment above date_create_immutable() with
  a two-line /* */ block.

@mikachan mikachan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates! I think we can bring this in 🎉

@mikachan
mikachan enabled auto-merge (squash) November 19, 2026 10:56
@mikachan mikachan added the [Block] Post Date Affects the Post Date Block label Nov 19, 2026
@mikachan
mikachan merged commit 0ce49b5 into WordPress:trunk Nov 19, 2026
93 checks passed
@github-actions github-actions Bot added this to the Gutenberg 24.6 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Post Date Affects the Post Date Block First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Block library /packages/block-library [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Date block: manually-set custom date/time renders with double UTC offset on the front end

2 participants