Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/comment-date/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
Expand Down
60 changes: 60 additions & 0 deletions phpunit/blocks/render-block-comment-date-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Comment Date block rendering tests.
*
* @package WordPress
* @subpackage Blocks
*/

/**
* Tests for the Comment Date block.
*
* @group blocks
*
* @covers ::gutenberg_render_block_core_comment_date
*/
class Tests_Blocks_Render_Comment_Date extends WP_UnitTestCase {

/**
* Tests that the human-diff format measures the elapsed time from the actual Unix timestamp.
*
* The local 'U' timestamp from {@see mysql2date()} includes the timezone offset, so a site
* ahead of UTC would otherwise report a comment as hours older than it is.
*
* @dataProvider data_gmt_offsets
*
* @param int $gmt_offset GMT offset in hours.
*/
public function test_human_diff_ignores_timezone_offset( $gmt_offset ) {
update_option( 'timezone_string', '' );
update_option( 'gmt_offset', $gmt_offset );

$timestamp = time() - 10 * MINUTE_IN_SECONDS;
$comment_id = self::factory()->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( '<!-- wp:comment-date {"format":"human-diff"} /-->' )[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<non-falsy-string, array{ gmt_offset: int }>
*/
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 ),
);
}
}
Loading