Skip to content

Instantly share code, notes, and snippets.

@JulesWang
Forked from fukata/pretty_date.php
Created March 26, 2011 10:47
Show Gist options
  • Save JulesWang/888194 to your computer and use it in GitHub Desktop.
Save JulesWang/888194 to your computer and use it in GitHub Desktop.
function pretty_date($time) {
$diff = time() - $time;
$day_diff = floor($diff / 86400);
if(is_nan($day_diff)) return '';
if ($day_diff == 0) {
if ($diff < 60) {
return $diff . "second ago";
} else if ($diff < 120) {
return '1 min ago';
} else if ($diff < 3600) {
return floor( $diff / 60 ) . " min ago";
} else if ($diff < 7200) {
return '1 hour ago';
} else if ($diff < 86400) {
return floor( $diff / 3600 ) . " hour ago";
}
} else if ($day_diff == 1) {
return 'yesterday';
} else if ($day_diff < 7) {
return $day_diff . " day ago";
} else if ($day_diff < 31) {
return ceil( $day_diff / 7 ) . " week ago";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment