-
dConstruct and my sisters graduation, a busy week!
The banners are all printed and packed away, the badges, stickers and cards are sorted into little bowls, programs have been stickered and stuffed into little plastic wallets.
The atmosphere office-wide has been buzzing with industrious preparation (in addition to the studious client work) for the past few weeks now. Sophie is definitely the star of the conference however, the amount of herself she has given to the organisation of dConstruct is almost super human and I am sure its going to be a great conference because of it.
The speakers have already begun to arrive and the first workshops start fresh and early at 10am tommorow morning downstairs in the Digital Lounge. I will be on the door for registration and helping out with the 'Managing Community, by Design' workshop run by Lane Becker, Thor Muller and Leslie Chicoine which I am quite excited about.
Late wednesday night I will be packing Simon off to Django Con and then dashing off up to London to my sister Louise's graduation from Fine Art at Goldsmiths. I am immensely proud of her and looking forward to seeing her all gowned up and I fully intend to fuss over her as only a big sister can. I will then be dashing back to continue helping set up the dome for the main dConstruct event. The pre-party stats at 8pm in Po Na Na on East Street and I hope to see you there!
Friday will be an extraviganza of 'Designing the Social Web' with an exciting lineup of speakers and talks. We were lucky enough to get a preview of Jeremy's yesterday and I think you are in for a treat.
Once the learning is over the after-party in 'Above Audio' can begin, no better way to end a very busy week and an excuse to socialise with the tons of interesting people who are descending on Brighton this week
-
One from the archive: resurrecting time_since()
Since the disastrous demise of my once dependable hosting company, I have lost a lot of data and subsequently broken permalinks to various entries of mine around the internets. Using the wonders of the Internet archive and the wayback machine I managed to ferret out one of my older posts that got a lot of coverage back in June 2003.
Yep, I know that was a long time ago now, I was a 21 year old, fresh faced computer scientist and this was one of the first bits of PHP I wrote and then released (the very first was a ShoutBox application), my first languages being Visual Basic and Java. I am not republishing this as an indication of my current coding ability or for any other self publicising reason. I decided to resurrect this post exactly as it was back then, mainly so permalinks could be updated now I have a more stable hosting provider, but also to provide a bit of an update as to the function's usage over the years.
Since its initial launch, the time_since() function has been translated into various different languages, both spoken (of which I can only find links to the German translation) and written programming languages - many of which are much improvised and improved implementations.
- ASP
- JavaScript
- Perl
- Python (or here)
But what I am most pleased with, if you can excuse my temporary lack of British modesty, was its inclusion in various notable applications or plugins for applications.
- Django - The popular content management framework written in Python.
- Feedster - Scott Johnson has written some very nice things about the time_since function and used it in Feedster.
- Blogroll - Keith Devens blogroll implementation, I am not sure if he released it or not though.
- Wordpress Plugins - various wordpress plugins, one by Michael Heilemann, and another by some guy who only mentions me by my alter-ego 'Simon Willison's Girlfriend' and not by my actual name.
- Wordpress core - can't be 100% certain about the derivation of this one but I'm pretty sure the timesince function has made its way into the wordpress core by way of the
human_time_difffunction. - Expression Engine - A plugin for the Expression Engine content management system written by Steve Sharpe.
- TWiki - Plugin for the TWiki engine.
- Xaraya - Another content management system written in php, with details of how to use.
So, without further ado, and with great respect to everyone who has altered, re-written and improved the function over the past 3 or 4 years ... here is the post as originally presented.
The original post ... "The time_since() function"
I have changed the function that displays the 'time since' each blog entry, managing not only to reduce it from 68 lines of code to 41, but also making it more maintainable in the process.
The basic requirements were that it should display the following:
- minutes up until 1 hour,
- hours and minutes up until 5 hours
- hours up until 1 day
- days and hours up until 1 week
- weeks and days up until 1 month
- months until 1 year
- years and months until 5 years
- years after that.
This was fine but the code was messy, with 7 if-statements used to calculate minutes, hours etc. separately.
Looking at the algorithm closer (and ignoring items 2 and 7) I decided that a better way to represent it would be through a series of time periods or 'chunks', where it displays the largest and the second largest chunks that will fit into it (eg. for 9 years 6 months 2 weeks and 1 day it would display '9 years, 6 months').
Using this data structure as the core behaviour of the function, and representing it as an array of arrays, turned out to be a much neater way of doing things. The subarrays each have 2 elements in them, with the first element being the number of seconds in that time period, and the second being the singular name for that period. The first part of the function is as follows:
/* Works out the time since the entry post, takes a an argument in unix time (seconds) */ function time_since($original) { // array of time period chunks $chunks = array( array(60 * 60 * 24 * 365 , 'year'), array(60 * 60 * 24 * 30 , 'month'), array(60 * 60 * 24 * 7, 'week'), array(60 * 60 * 24 , 'day'), array(60 * 60 , 'hour'), array(60 , 'minute'), );After this we work out the number of seconds since the time passed to the function:
$today = time(); /* Current unix time in seconds */ $since = $today - $original;A '
for' loop is used find the largest 'chunk' by cycling through the '$chunks' array and checking if the number of chunks that fit in to the time period is not zero. This is why the '$chunks' array is reversed with the largest chunk first, so that if doesn't match that one it skips to the next.// $j saves performing the count function each time around the loop for ($i = 0, $j = count($chunks); $i < $j; $i++) { $seconds = $chunks[$i][0]; $name = $chunks[$i][1]; // finding the biggest chunk (if the chunk fits, break) if (($count = floor($since / $seconds)) != 0) { break; } }By having a '
$print' variable to output, it is possible to now use PHP's string concatenation '.=' operator to add the smaller chunk to the beginning of the '$print' variable. The 'floor()' function knocks off the remainder, so 2.5 becomes 2.$print = ($count == 1) ? '1 '.$name : "$count {$name}s";By using a ternary operator here, this can cope with having 1 week and x weeks, by checking if the number of chunks is one or not.
The next item in the array, '
$chunks[$i + 1]' is the next smaller time chunk, so we need to display how many of these occur in the time period once the number of seconds taken up in the first chunk have been accounted for.The second chunk is only added if its count is greater than zero; if it is not then the '
$print' variable only contains the number of the larger chunks that occur.if ($i + 1 < $j) { // now getting the second item $seconds2 = $chunks[$i + 1][0]; $name2 = $chunks[$i + 1][1]; // add second item if it's count greater than 0 if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) { $print .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s"; } }Now we can return the '
$print' variable and finish the function.return $print; }The full function can be found in this plain text file.
2 items tagged "event"
Look at "event" on del.icio.us, Flickr or Technorati
