Blog dates

Ignore the following if you’re not interested in WordPress themes, PHP date functions, or blog formatting…

Have you noticed how the way we are presented with the date has changed online? Examples include “5 hours ago”, “yesterday”, “Monday 5pm”. Personally I blame Facebook, but the result is rather friendlier that just a straight time stamp. I put together the following simple PHP function to generate Facebook style posting dates:

function facebookDate($time) {

  #Difference in seconds between now and the supplied time
  $difference = time()-$time;
  if ( $difference < 3600 ) {
    #Within last hour
    return round($difference/60).' minutes ago';
  } elseif ( $difference < 86400 ) {
    #Within last 24 hours
    return round($difference/3660).' hours ago';
  } elseif ( $time > strtotime('yesterday') ) {
    #Yesterday
    return 'Yesterday at '.date('H:i', $time);
  } elseif ( $difference < 604800 ) {
    #Within last week
    return date('D \a\t H:i', $time);
  } elseif ( $time > mktime(0, 0, 0, 1, 1, date("Y")) ) {
    #Since Jan 01 this year
    return date('d F \a\t H:i', $time);
  } else {
    return date('d F Y \a\t H:i', $time);
  }
}

Its not really rocket science, but it seems to work quite well. The only input it a standard UNIX timestamp which means you have to use

<?=facebookDate(get_the_time('U'));?>

if you’re using WordPress.