How to Add a Running Total of How Long You’ve Blogged To Thesis

UPDATE 04/04/2013: There’s some much easier code for this now. Check out the update post How Old Is Reactuate Exactly?

Doing some maintenance on my various blogs and remembered I used to have this code in my footer that showed how many years and days I’d been blogging. Spent way too much time looking for the code in old backups of this site.

Then I searched the site and found this post How to Add a Running Total of Years and Days You’ve Blogged. I use the Thesis theme now and so that code doesn’t work.

So this is an update for those who use thesis.

What to Put Where

1. Open your custom_functions.php. This is where you should make any changes to Thesis you are going to. Don’t go hacking the theme files. Then when you upgrade this code will stay in the footer.

2. Tell Thesis you want to replace the footer. We first have to add a line that tells Thesis we will write a routine to create the footer. So add this line towards the top of custom_functions.php:

add_action('thesis_hook_footer', 'custom_footer');

3. Write the code to replace the footer HTML. I don’t use the term code to describe HTML. For me code has to generate something and HTML just describes something. But we’re going to put in some PHP code to generate the days and years. And while we are there, we’ll make the copyright update too.

Here’s the whole function for the footer. Type or paste it into your custom_functions.php file and save. Then you’ll have the same footer I do. (Which you probably don’t want, so keep reading)

[cc lang=”php”]
function custom_footer() {
$beginDate = strtotime(‘October 28th, 2002’);
$diffTime = time() – $beginDate;
$days = round($diffTime/86400);
$years = round($days/ 356);
$daysLeft = $days – ($years * 365);

echo "<p>Blogging for ".$years." years and ".$daysLeft." days.</p>";
echo "<p>Copyright &copy 2002-".date( "Y" )." Ron Davis. All rights reserved.</p><br/>";
echo "<p>Get smart with the <a href=\"\">Thesis WordPress Theme</a> from DIYthemes.</p>";

Breaking It Down

1. Calculating the days and years. In my original code all the date calculation was in one line and part of the actual output. There was an error is this, so I broke it down too see what each part did.

$beginDate = strtotime('October 28th, 2002');
You have to know your first post’s date. Go look it up and replace my date with yours. The code converts that into a time format PHP likes ($beginDate) which is the number of seconds since whenever php starts keeping time.

$diffTime = time() - $beginDate;
Then we subtract that begin date from the current time. This gives us the total number of seconds you’ve been blogging. Not a very useful thing for people to read.

$days = round($diffTime/86400);
Next we do some division to get the number of days you’ve been blogging. In the original code I had it broken down for each subunit as part of the division. I just made it into one big number. The number of seconds in a day and divide our blogging seconds by that. Since this probably won’t be even, we just round off using the php round() function.

$years = round($days/ 356);
Now we have the number of days we’ve been blogging. Dividing that by 365 gives us the number of years. Not perfect – doesn’t take into account leap years – but close.

$daysLeft = $days - ($years * 365);
Then we take this number of years and subtract the days of the whole years from our total days. What’s left is how many days we’ve been blogging in the current year.

2. Make Your Footer HTML.The next part is just generating the HTML for the footer using the days and years we calculated. Remember this routine is in php, so you have to surround you HTML with an echo call.

echo "<p>Blogging for ".$years." years and ".$daysLeft." days.</p>";
The first line is the meat of what we created. It says we’ve blogged for so many years and day.

echo "<p>Copyright &copy 2002-".date( "Y" )." Ron Davis. All rights reserved.</p><br/>";
Then we display my copyright. And since I know the current year, I went ahead and used it so I don’t have to update the copyright. (I’m not sure that’s even needed, but it’s cool.)

echo "<p>Get smart with the <a href=\"\">Thesis WordPress Theme</a> from DIYthemes.</p>";
Lastly I put a link to Thesis – a great flexible theme – with my affiliate link. It’s a good example of how you can put anything you want in there.

One Comment

  1. The Triumph says:

    Thanks for the code.

Comments are closed.