Skip to content

Instantly share code, notes, and snippets.

@jonnymaceachern
Last active September 3, 2015 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonnymaceachern/55b1364da57d49486c26 to your computer and use it in GitHub Desktop.
Save jonnymaceachern/55b1364da57d49486c26 to your computer and use it in GitHub Desktop.
Modified version of the WordPress human_time_diff() timestamp. Example: It is Sept 7th and the function is given a duration threshold of 7 days. If the post you are retrieving a timestamp from was made anytime after Sept 1st, it will return a time difference (e.g. 4 days, 3 hrs, 15 minutes, 24 seconds, etc). If you are retrieving a timestamp fro…
/**
* Modified human_time_diff timestamp
* @param integer $duration (days) A threshold. If exceeded, the timestamp will return a date rather than a human_time_diff
* @return string A human-friendly timestamp
*
* credit: http://www.binarymoon.co.uk/2013/12/wordpress-improved-human-time-difference/
*
*/
function human_time_diff_enhanced( $duration = 60 ) {
$post_time = get_the_time('U');
$human_time = '';
$time_now = date('U');
// use human time if less that $duration days ago (60 days by default)
// 60 seconds * 60 minutes * 24 hours * $duration days
if ( $post_time > $time_now - ( 60 * 60 * 24 * $duration ) ) {
$human_time = sprintf( __( '%s ago', 'binarymoon'), human_time_diff( $post_time, current_time( 'timestamp' ) ) );
} else {
$human_time = get_the_date();
}
return $human_time;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment