Create Twitter like date formatted strings with PHP

by Glyn Mooney

Well I got a little bored and decided to code this function. It creates the old Twitter like date format i.e. About 4 minutes ago, etc.. As you can see the code below has a few tests so you can decipher how to use it.

<?php
#If your running PHP > 5.3 you need to set this or your app will throw errors
date_default_timezone_set('Europe/London');

function twitter_time_format ($date) {

$blocks = array (
array('year',  (3600 * 24 * 365)),
array('month', (3600 * 24 * 30)),
array('week',  (3600 * 24 * 7)),
array('day',   (3600 * 24)),
array('hour',  (3600)),
array('min',   (60)),
array('sec',   (1))
);

#Get the time from the function arg and the time now
$argtime = strtotime($date);
$nowtime = time();

#Get the time diff in seconds
$diff    = $nowtime - $argtime;

#Store the results of the calculations
$res = array ();

#Calculate the largest unit of time
for ($i = 0; $i < count($blocks); $i++) {      $title = $blocks[$i][0];      $calc  = $blocks[$i][1];      $units = floor($diff / $calc);      if ($units > 0) {
$res[$title] = $units;
}
}

if (isset($res['year']) && $res['year'] > 0) {
if (isset($res['month']) && $res['month'] > 0 && $res['month'] < 12) {        $format      = "About %s %s %s %s ago";         $year_label  = $res['year'] > 1 ? 'years' : 'year';
$month_label = $res['month'] > 1 ? 'months' : 'month';
return sprintf($format, $res['year'], $year_label, $res['month'], $month_label);
} else {
$format     = "About %s %s ago";
$year_label = $res['year'] > 1 ? 'years' : 'year';
return sprintf($format, $res['year'], $year_label);
}
}

if (isset($res['month']) && $res['month'] > 0) {
if (isset($res['day']) && $res['day'] > 0 && $res['day'] < 31) {        $format      = "About %s %s %s %s ago";         $month_label = $res['month'] > 1 ? 'months' : 'month';
$day_label   = $res['day'] > 1 ? 'days' : 'day';
return sprintf($format, $res['month'], $month_label, $res['day'], $day_label);
} else {
$format      = "About %s %s ago";
$month_label = $res['month'] > 1 ? 'months' : 'month';
return sprintf($format, $res['month'], $month_label);
}
}

if (isset($res['day']) && $res['day'] > 0) {
if ($res['day'] == 1) {
return sprintf("Yesterday at %s", date('h:i a', $argtime));
}
if ($res['day'] <= 7) {
return date("\L\a\s\\t l \a\\t h:i a", $argtime);
}
if ($res['day'] <= 31) {         return date("l \a\\t h:i a", $argtime);       }     }         if (isset($res['hour']) && $res['hour'] > 0) {
if ($res['hour'] > 1) {
return sprintf("About %s hours ago", $res['hour']);
} else {
return "About an hour ago";
}
}

if (isset($res['min']) && $res['min']) {
if ($res['min'] == 1) {
return "About one minut ago";
} else {
return sprintf("About %s minuts ago", $res['min']);
}
}

if (isset ($res['sec']) && $res['sec'] > 0) {
if ($res['sec'] == 1) {
return "One second ago";
} else {
return sprintf("%s seconds ago", $res['sec']);
}
}
}

$now            = date(DATE_RFC2822, time());
$anhourago      = date(DATE_RFC2822, time() - (3600));
$fourhoursago   = date(DATE_RFC2822, time() - (3600 * 4));
$yesterday      = date(DATE_RFC2822, time() - (3600 * 24));
$lastweek       = date(DATE_RFC2822, time() - (3600 * 24 * 6));
$lastmonth      = date(DATE_RFC2822, time() - (3600 * 24 * 31));
$threemonthsago = date(DATE_RFC2822, time() - (3600 * 24 * 31 * 3));
$ayearago       = date(DATE_RFC2822, time() - (3600 * 24 * 365));
$fiveyearsago   = date(DATE_RFC2822, time() - (3600 * 24 * 365 * 5));

echo 'Now (should be empty): '              . twitter_time_format($now) . '
'
;
echo 'An Hour Ago: '      . twitter_time_format($anhourago) . '
'
;
echo 'Four Hours Ago: '   . twitter_time_format($fourhoursago) . '
'
;
echo 'Yesterday: '        . twitter_time_format($yesterday) . '
'
;
echo 'Last Week: '        . twitter_time_format($lastweek) . '
'
;
echo 'Last Month: '       . twitter_time_format($lastmonth) . '
'
;
echo 'Three Months Ago: ' . twitter_time_format($threemonthsago) . '
'
;
echo 'A Year Ago: '       . twitter_time_format($ayearago) . '
'
;
echo 'Five Years Ago: '   . twitter_time_format($fiveyearsago) . '
'
;
?>

Hope this helps!