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 '';
}
}
@smeghead
Copy link

2週間以上過去のものは、日付を表示するようになる方が、逆にわかりやすいんじゃないかと思うんですが、どうですかね。。

@fukata
Copy link
Author

fukata commented Mar 26, 2011

なるほど。まぁ、たしかに2週間前とか言われても何時?という感じではありますね。1 month ago (yyyy.mm.dd)的なのはどうでしょう。欲張りすぎ?w

@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