Ever need to provide a fuzzy date using PHP for your web app? Heres a simple snippet that will do just that.
<?php
function fuzzy_date($timestamp)
{
$myDays = array("Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat");
if( preg_match("/[-\/:]/", $timestamp) )
$timestamp = strtotime($timestamp);
if($timestamp > time())
// All future dates
return date('m/d/y', $timestamp);
elseif($timestamp >= mktime(0,0,0))
// Today
return 'Today';
elseif($timestamp >= mktime(0,0,0) - 86400)
// Yesterday
return 'Yesterday';
elseif($timestamp >= mktime(0,0,0) - 86400*7)
// Within 7 days
return $myDays[date('w', $timestamp)];
elseif($timestamp >= mktime(0,0,0,1,1))
// Within 1 year
return date('M d', $timestamp);
else
// Older than 1 year
return date('m/d/y', $timestamp);
}
// Run it for the past month
foreach(range(0,31) as $day) printf("%s<br/>", fuzzy_date(time() - ($day * 86400)));
?>