diff --git a/packages/block-library/CHANGELOG.md b/packages/block-library/CHANGELOG.md index ace11d214b889c..36469b31b2532a 100644 --- a/packages/block-library/CHANGELOG.md +++ b/packages/block-library/CHANGELOG.md @@ -10,6 +10,7 @@ - Tabs: Open the tab panels when an HTML anchor on the Tabs block or a Tab Panel contains a character such as `&` or `'`, instead of leaving them hidden ([#86415](https://github.com/WordPress/gutenberg/pull/86415)). - Tabs: Keep the default tab on the same tab when tabs are added, removed or reordered, and show the first tab when the saved default tab no longer exists, instead of hiding every panel ([#86362](https://github.com/WordPress/gutenberg/pull/86362)). - Tabs: Swap the Left and Right arrow keys on RTL sites, so each key moves focus to the tab it points to ([#86429](https://github.com/WordPress/gutenberg/pull/86429)). +- Comment Date: Fix the "human-diff" format being off by the site's timezone offset ([#86398](https://github.com/WordPress/gutenberg/pull/86398)). ## 12.1.0 (2026-11-04) diff --git a/packages/block-library/src/comment-date/index.php b/packages/block-library/src/comment-date/index.php index dd8beb9dabb000..4aae53e0d6fc18 100644 --- a/packages/block-library/src/comment-date/index.php +++ b/packages/block-library/src/comment-date/index.php @@ -34,7 +34,8 @@ function render_block_core_comment_date( $attributes, $content, $block ) { $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) ); if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) { - $timestamp = get_comment_date( 'U', $comment ); + // The GMT time is needed, since the 'U' format of the local time includes the timezone offset. + $timestamp = get_comment_time( 'U', true, false, $comment ); if ( false === $timestamp ) { return ''; } diff --git a/phpunit/blocks/render-block-comment-date-test.php b/phpunit/blocks/render-block-comment-date-test.php new file mode 100644 index 00000000000000..6a9e8f2f4ea20c --- /dev/null +++ b/phpunit/blocks/render-block-comment-date-test.php @@ -0,0 +1,60 @@ +comment->create( + array( + 'comment_post_ID' => self::factory()->post->create(), + 'comment_date' => gmdate( 'Y-m-d H:i:s', $timestamp + $gmt_offset * HOUR_IN_SECONDS ), + 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $timestamp ), + ) + ); + + $parsed_block = parse_blocks( '' )[0]; + $block = new WP_Block( $parsed_block, array( 'commentId' => $comment_id ) ); + + $this->assertSame( '10 minutes ago', wp_strip_all_tags( $block->render() ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_gmt_offsets() { + return array( + 'UTC' => array( 'gmt_offset' => 0 ), + 'UTC+2' => array( 'gmt_offset' => 2 ), + 'UTC-5' => array( 'gmt_offset' => -5 ), + 'UTC+14' => array( 'gmt_offset' => 14 ), + ); + } +}