For most websites, content or have a shelf life longer than a few minutes. Indeed, popular posts can be visited for months or even years. It is therefore important that any content that is seen, is kept up-to-date, so that is still suitable for visitors.
This then brings the issue of where the content shows on the lists of posts. Here at WP Business Club we use the WP types pluggin for content design, where we were therefore able to change the post lists to work on the basis of date of update rather than date of publish. This can also be done by editing the PHP code of the theme.
We also then decided that posts should display the date of update, rather than date published. To take this to the next level, we then found the code which converts the date published into a time period. Recent posts may they for have the entry, ‘updated: one hour ago’, or ‘updated: five days ago’.
The solution we are using is to add code was posted by Jason Bobich some 5 years ago, which demonstrates that old posts can still be useful, and we added this code into the functions.php file of our theme. We can then use a shortcode to display the time. The php code is shown below. If you want to know how the code shown below looks so good, well that is because we are using the free plugin JSJ code highlight.
function themeblvd_time_ago() {
global $post;
$date = get_post_time('G', true, $post);
/**
* Where you see 'themeblvd' below, you'd
* want to replace those with whatever term
* you're using in your theme to provide
* support for localization.
*/
// Array of time period chunks
$chunks = array(
array( 60 * 60 * 24 * 365 , __( 'year', 'themeblvd' ), __( 'years', 'themeblvd' ) ),
array( 60 * 60 * 24 * 30 , __( 'month', 'themeblvd' ), __( 'months', 'themeblvd' ) ),
array( 60 * 60 * 24 * 7, __( 'week', 'themeblvd' ), __( 'weeks', 'themeblvd' ) ),
array( 60 * 60 * 24 , __( 'day', 'themeblvd' ), __( 'days', 'themeblvd' ) ),
array( 60 * 60 , __( 'hour', 'themeblvd' ), __( 'hours', 'themeblvd' ) ),
array( 60 , __( 'minute', 'themeblvd' ), __( 'minutes', 'themeblvd' ) ),
array( 1, __( 'second', 'themeblvd' ), __( 'seconds', 'themeblvd' ) )
);
if ( !is_numeric( $date ) ) {
$time_chunks = explode( ':', str_replace( ' ', ':', $date ) );
$date_chunks = explode( '-', str_replace( ' ', '-', $date ) );
$date = gmmktime( (int)$time_chunks[1], (int)$time_chunks[2], (int)$time_chunks[3], (int)$date_chunks[1], (int)$date_chunks[2], (int)$date_chunks[0] );
}
$current_time = current_time( 'mysql', $gmt = 0 );
$newer_date = strtotime( $current_time );
// Difference in seconds
$since = $newer_date - $date;
// Something went wrong with date calculation and we ended up with a negative date.
if ( 0 > $since )
return __( 'sometime', 'themeblvd' );
/**
* We only want to output one chunks of time here, eg:
* x years
* xx months
* so there's only one bit of calculation below:
*/
//Step one: the first chunk
for ( $i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
// Finding the biggest chunk (if the chunk fits, break)
if ( ( $count = floor($since / $seconds) ) != 0 )
break;
}
// Set output var
$output = ( 1 == $count ) ? '1 '. $chunks[$i][1] : $count . ' ' . $chunks[$i][2];
if ( !(int)trim($output) ){
$output = '0 ' . __( 'seconds', 'themeblvd' );
}
$output .= __(' ago', 'themeblvd');
return $output;
}
// Filter our themeblvd_time_ago() function into WP's the_time() function
add_filter('the_time', 'themeblvd_time_ago');
Now, whenever a post is updated, it will move back to the top of the list and its new update time will be shown.