14 Feb 2006
I write a fair amount about my new daughter. Becoming a dad has shifted my life around quite a bit. As I write things about when she first smiled, or first rolled over, it was keeping the date of when those things happened, but the I’d have to do date-math in my head to figure out how old she was. I don’t like doing date-math in my head. So, I wrote a wordpress plugin to do it for me. This plugin adds text to the bottom of each post about my daughter saying how old she was when that post was written. It is made general enough, however, to be about the duration to or from any particular event you care to track.
At the moment, the Duration plugin has only been tested with wordpress 1.5.2 which is what ChoicyWhiteBoy is using. I have no reason to believe that it won’t work with 2.0x, but I’ve not yet tried it.
The following is from the readme included with duration:
This is version 1.0 of the duration plugin
The duration plugin adds a line of text at the end of any posts that
are in whatever categories you define for it. That line of text
includes the duration (in years, months, days, hours, minutes and even
seconds if you choose) between when that post was written and some
other specific event of your choosing. The duration works either way
— forward or backward, meaning that the event can be in the past, or
the future. However, there is currently no provision for an event that
happens during the blogging time. Meaning that the same text is
printed regardless of if some posts are earlier than the event while
others are later. Perhaps a future version of this plugin will
address that shortcoming. The duration plugin allows you to specify
some text both before and after the duration string, all of which is
surrounded by a paragraph with an id of ‘duration’ so that you can
style it in your CSS however you like. You can also specify the
granularity you would like to display from year all the way down to
Unpack the .zip (or .tar.gz) file in the wordpress/wp-content/plugins
directory. It will create duration.php and a duration subdirectory
which contains ancillary support files.
Go to the ‘Plugins’ administrative page, and select ‘activate’ for the
Go to the Options Page, and select the ‘Duration’ submenu.
You shouldn’t need to change any HTML or source code to use the
From the Options page, with the ‘Duration’ submenu chosen, you can
choose which categories you would like the duration string to appear
on. It will appear on any post that is categorized with any of the
You can write text that will appear before and after the duration
string. For example, I use it to track how old my daughter was when I
post something about her. Therefore, I have the following text for
before: “(Sam was”
and for after: “old when this post was written.)”
This produces text like:
(Sam was 4 months, 11 days, and 15 hours old when this post was
You then select the date and time of the event you wish to track. Use
all four digits for the year. There’s not a great deal of error
checking, so if you try to screw it up, you’ll probably manage to.
Finally, you can select the level of precision that it should
report. It can include just the year — for example “It was 230 years
since the signing of the American Declaration of Independence”, or you
can go as detailed as to include the seconds — for example “As of
this post, it has been 1 year, 2 months, 4 days, 7 hours, and 15
seconds since I slept for more than 5 hours at a time.”
Once you are happy with your selections, click “Update Options.” This
will write your choices to the database. View your site to see it in
This is my first wordpress plugin. It should be fairly well commented
if you want to use it as an example for your first plugin, but I’m not
a professional programmer, and some of the code is fairly messy. I
will make an effort to provide some support for this plugin, but as I
have a very young child in the house, that support may be spotty. Use
at your own risk.
* No difference between events in the past and future
* English Only (no internationalization)
* Assumes you are in the same time zone as your event
* Only one event is possible (heaven forbid you have 2+ children! 😉
I don’t have any work-arounds for any of the known issues. I may be
able to address some of them in future versions, but as I’ve met my
needs, my motivation to continue work on this particular plugin is
rather low. Feel free to bribe me to add your favorite feature or fix
your most annoying bug though.
[Update Mar. 2, 2006]
I found a bug where I was miscalculating the number of days in a month for subtraction. I’ve patched it for my own site, but with a kludge where I arbitrarily set it to 30 days. I need to figure out how to determine which month it’s being taken from, and then take the right number of days. I’ll release a new version once I’ve done this.