Skip to content

Instantly share code, notes, and snippets.

@fukata
Created March 26, 2011 10:43
Show Gist options
  • Save fukata/888192 to your computer and use it in GitHub Desktop.
Save fukata/888192 to your computer and use it in GitHub Desktop.
Twitter has been used in such a function to generate a pretty date format string.
<?php
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";
} else {
return '';
}
}
@wonjun27
Copy link

wonjun27 commented Nov 6, 2013

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment