[wp-trac] [WordPress Trac] #29849: Better human_time_diff()
WordPress Trac
noreply at wordpress.org
Wed Jun 24 19:23:50 UTC 2015
#29849: Better human_time_diff()
-------------------------+------------------------------
Reporter: kovshenin | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Date/Time | Version:
Severity: normal | Resolution:
Keywords: has-patch | Focuses:
-------------------------+------------------------------
Comment (by akirk):
Maybe we can add some momentum to this again: `human_time_diff()` is often
used in a wrong way, beginning with the example in the
[http://codex.wordpress.org/Function_Reference/human_time_diff Codex]: it
suggests a string concattenation of `ago`:
`<?php echo human_time_diff( get_the_time('U'), current_time('timestamp')
) . ' ago'; ?>`
I'd argue that this is the only way that people use `human_time_diff()`,
many don't know or care that a `printf( __( '%s ago' ), human_time_diff(
$timestamp ) );` is needed, and use the `. ' ago'` from the example.
`wp_natural_time()` would handle this out of the box, while including the
other optimizations by @kovshenin. I have changed one thing in the
behavior though: it only allows adjacent time units to avoid output like
"1 year, 3 minutes ago".
I have added two diffs, one that shows where `human_time_diff()` could be
replaced in the current codebase, the other one incorporates the
suggestions by @johnbillon and also adds unit tests.
I'd also suggest to deprecate `human_time_diff()` and suggest the usage of
this new function, also update the Codex and remove the misleading example
of string concattenation.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/29849#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list