Last active
September 3, 2015 16:42
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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