Skip to content

Commit c6dc9c6

Browse files
author
Paul Bearne
committed
Replace human_time_diff with wp_natural_time
Updated various instances where human_time_diff was used to calculate human-readable time differences to use the more flexible wp_natural_time function instead. This change simplifies the code and leverages the improved functionality of wp_natural_time for a more natural language output.
1 parent 54ce901 commit c6dc9c6

File tree

7 files changed

+10
-12
lines changed

7 files changed

+10
-12
lines changed

src/wp-admin/includes/class-wp-media-list-table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ public function column_date( $post ) {
544544

545545
if ( $time && $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) {
546546
/* translators: %s: Human-readable time difference. */
547-
$h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
547+
$h_time = wp_natural_time( $time );
548548
} else {
549549
$h_time = get_the_time( __( 'Y/m/d' ), $post );
550550
}

src/wp-admin/includes/class-wp-plugin-install-list-table.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,7 @@ public function display_rows() {
701701
<div class="column-updated">
702702
<strong><?php _e( 'Last Updated:' ); ?></strong>
703703
<?php
704-
/* translators: %s: Human-readable time difference. */
705-
printf( __( '%s ago' ), human_time_diff( $last_updated_timestamp ) );
704+
echo wp_natural_time( $last_updated_timestamp );
706705
?>
707706
</div>
708707
<div class="column-downloaded">

src/wp-admin/includes/class-wp-privacy-requests-table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ protected function get_timestamp_as_date( $timestamp ) {
479479

480480
if ( $time_diff >= 0 && $time_diff < DAY_IN_SECONDS ) {
481481
/* translators: %s: Human-readable time difference. */
482-
return sprintf( __( '%s ago' ), human_time_diff( $timestamp ) );
482+
return wp_natural_time( $timestamp );
483483
}
484484

485485
return date_i18n( get_option( 'date_format' ), $timestamp );

src/wp-admin/includes/plugin-install.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,7 @@ function install_plugin_information() {
663663
<?php } if ( ! empty( $api->last_updated ) ) { ?>
664664
<li><strong><?php _e( 'Last Updated:' ); ?></strong>
665665
<?php
666-
/* translators: %s: Human-readable time difference. */
667-
printf( __( '%s ago' ), human_time_diff( strtotime( $api->last_updated ) ) );
666+
echo wp_natural_time( $api->last_updated );
668667
?>
669668
</li>
670669
<?php } if ( ! empty( $api->requires ) ) { ?>

src/wp-admin/includes/revision.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ function wp_prepare_revisions_for_js( $post, $selected_revision_id, $from = null
251251
'date' => date_i18n( __( 'M j, Y @ H:i' ), $modified ),
252252
'dateShort' => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), $modified ),
253253
/* translators: %s: Human-readable time difference. */
254-
'timeAgo' => sprintf( __( '%s ago' ), human_time_diff( $modified_gmt, $now_gmt ) ),
254+
'timeAgo' => wp_natural_time( $modified_gmt, $now_gmt ),
255255
'autosave' => $autosave,
256256
'current' => $current,
257257
'restoreUrl' => $can_restore ? $restore_link : false,
@@ -294,7 +294,7 @@ function wp_prepare_revisions_for_js( $post, $selected_revision_id, $from = null
294294
'date' => date_i18n( __( 'M j, Y @ H:i' ), strtotime( $post->post_modified ) ),
295295
'dateShort' => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), strtotime( $post->post_modified ) ),
296296
/* translators: %s: Human-readable time difference. */
297-
'timeAgo' => sprintf( __( '%s ago' ), human_time_diff( strtotime( $post->post_modified_gmt ), $now_gmt ) ),
297+
'timeAgo' => wp_natural_time( $post->post_modified_gmt, $now_gmt ),
298298
'autosave' => false,
299299
'current' => true,
300300
'restoreUrl' => false,

src/wp-includes/blocks/comment-date.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function render_block_core_comment_date( $attributes, $content, $block ) {
3030
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
3131
if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) {
3232
// translators: %s: human-readable time difference.
33-
$formatted_date = sprintf( __( '%s ago' ), human_time_diff( get_comment_date( 'U', $comment ) ) );
33+
$formatted_date = wp_natural_time( get_comment_date( 'U', $comment ) );
3434
} else {
3535
$formatted_date = get_comment_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $comment );
3636
}

src/wp-includes/blocks/post-date.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ function render_block_core_post_date( $attributes, $content, $block ) {
2626
$post_timestamp = get_post_timestamp( $post_ID );
2727
if ( $post_timestamp > time() ) {
2828
// translators: %s: human-readable time difference.
29-
$formatted_date = sprintf( __( '%s from now' ), human_time_diff( $post_timestamp ) );
29+
$formatted_date = wp_natural_time( $post_timestamp );
3030
} else {
3131
// translators: %s: human-readable time difference.
32-
$formatted_date = sprintf( __( '%s ago' ), human_time_diff( $post_timestamp ) );
32+
$formatted_date = wp_natural_time( $post_timestamp );
3333
}
3434
} else {
3535
$formatted_date = get_the_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );
@@ -52,7 +52,7 @@ function render_block_core_post_date( $attributes, $content, $block ) {
5252
if ( get_the_modified_date( 'Ymdhi', $post_ID ) > get_the_date( 'Ymdhi', $post_ID ) ) {
5353
if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) {
5454
// translators: %s: human-readable time difference.
55-
$formatted_date = sprintf( __( '%s ago' ), human_time_diff( get_post_timestamp( $post_ID, 'modified' ) ) );
55+
$formatted_date = wp_natural_time( get_post_timestamp( $post_ID, 'modified' ) );
5656
} else {
5757
$formatted_date = get_the_modified_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );
5858
}

0 commit comments

Comments
 (0)