Created
March 26, 2011 10:43
-
-
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.
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 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 ''; | |
} | |
} |
なるほど。まぁ、たしかに2週間前とか言われても何時?という感じではありますね。1 month ago (yyyy.mm.dd)的なのはどうでしょう。欲張りすぎ?w
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2週間以上過去のものは、日付を表示するようになる方が、逆にわかりやすいんじゃないかと思うんですが、どうですかね。。