
With the recent update to Hack Social Media’s theme which you can read over here, you might’ve noticed that the time stamp on blog posts no longer display the actual date such as “December 12, 2011.” In its place are relative dates popularized by Twitter where tweets were shown with dates according to how long ago since the tweet was published(2 hours ago).
Using a slightly modified piece of code I found via this blog post, I was able to convert the blog posts to display relative dates. Here’s the main PHP code:
if(!function_exists('how_long_ago')){
function how_long_ago($timestamp){
$difference = current_time('timestamp') - $timestamp;
if($difference >= 60*60*24*365){ // if more than a year ago
$int = intval($difference / (60*60*24*365));
$s = ($int > 1) ? 's' : '';
$r = $int . ' year' . $s . ' ago';
} elseif($difference >= 60*60*24*7*5){ // if more than five weeks ago
$int = intval($difference / (60*60*24*30));
$s = ($int > 1) ? 's' : '';
$r = $int . ' month' . $s . ' ago';
} elseif($difference >= 60*60*24*7){ // if more than a week ago
$int = intval($difference / (60*60*24*7));
$s = ($int > 1) ? 's' : '';
$r = $int . ' week' . $s . ' ago';
} elseif($difference >= 60*60*24){ // if more than a day ago
$int = intval($difference / (60*60*24));
$s = ($int > 1) ? 's' : '';
$r = $int . ' day' . $s . ' ago';
} elseif($difference >= 60*60){ // if more than an hour ago
$int = intval($difference / (60*60));
$s = ($int > 1) ? 's' : '';
$r = $int . ' hour' . $s . ' ago';
} elseif($difference >= 60){ // if more than a minute ago
$int = intval($difference / (60));
$s = ($int > 1) ? 's' : '';
$r = $int . ' minute' . $s . ' ago';
} else { // if less than a minute ago
$r = 'moments ago';
}
return $r;
}
}
The code above creates a function called “how_long_ago.” First, it determines the time difference between the current time and the time the particular blog post was published. This value is stored in a variable called $difference. Then, the function uses a if…else if…else block to determine what date to output based on the value of $difference.
Place the code above in your functions.php file inside a <?php and ?>block. In order to display the relative dates in your blog post, open up one of your theme files where the date is currently being outputted, such as the single.php or index.php file. Then, replace the existing function that returns the dates of your blog post, if it is there, and insert the following:
<?php if(!function_exists('how_long_ago')){the_time('F jS, Y'); } else { echo how_long_ago(get_the_time('U')); } ?>
The above code first checks if the function “how_long_ago” does not exist. If that is true, it will display the regular timestamp(December 12, 2010). Otherwise, the function does exist, therefore the relative timestamp is shown. This check is necessary as a means to avoid the “white screen of death” if you happened to delete your “how_long_ago” function.
Wow, what a great script… just what I needed. Thanks for sharing.
1 year ago
Thanks for sharing. Works great on my site
11 months ago
I was looking for this a long time and it’s the first function that works well. Thank you for sharing.
Sorry for my english, i’m still learning this great language.
7 months ago
Here’s a modified version of the code with proper i18n: http://pastebin.com/GBnSD9r5
3 months ago