Skip to content

Instantly share code, notes, and snippets.

@adamdehaven
Last active September 28, 2015 17:13
Show Gist options
  • Save adamdehaven/a724a184b1e3c96baf9d to your computer and use it in GitHub Desktop.
Save adamdehaven/a724a184b1e3c96baf9d to your computer and use it in GitHub Desktop.
Wordpress function to insert post or comment timestamp as relative (i.e. 2 weeks ago), or as date, depending on $duration (Default 60 days)
<?php
// Show relative time if more recent than 60 days for post or comment
function adamdehaven_human_time_diff( $type = 'post', $duration = 60 ) {
if($type == 'comment') {
$post_time = get_comment_time('U');
} else {
$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 ) ) {
if($type == 'comment') {
$human_time = sprintf( __( '%s ago', 'adamdehaven'), human_time_diff( $post_time, current_time( 'timestamp' ) ) );
} else {
$human_time = sprintf( __( 'Posted %s ago', 'adamdehaven'), human_time_diff( $post_time, current_time( 'timestamp' ) ) );
}
} else {
if($type == 'comment') {
$human_time = get_comment_date().' at '.get_comment_time('g:i A');
} else {
$human_time = get_the_date();
}
}
echo $human_time;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment