Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PHP fuzzy date function
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);
// 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)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.