Created
February 11, 2010 15:01
-
-
Save robhurring/301594 to your computer and use it in GitHub Desktop.
PHP fuzzy date function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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))); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment