引数に文字列の日付を入れることで、
- 30日以内なら「○日前」
- 1年以内なら「○ヶ月前」
- 10年以内なら「○年と○ヶ月前」
- それ以前なら「○年前」
と表示される関数です。
参考前でに。
エラーを返す場合:
日付データが読み込めなかったり、将来の日付は何も返しません。
関数例 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
function pastsince($value){ $timestamp = strtotime($value); if($timestamp<>""){ $totaltimestamp = time() - $timestamp; if($totaltimestamp > 0){ if($totaltimestamp <= 30*86400){ //1年前か $day = floor($totaltimestamp / 86400); return $day."日前"; }elseif($totaltimestamp <= 365*86400){ //1年前か $days = floor($totaltimestamp / 86400); $month = floor($days / 12); $day = $days % 12; //return $month."ヶ月と".$day."日前"; return $month."ヶ月前"; }elseif($totaltimestamp <= 365*86400*10){ //10年前か $days = floor($totaltimestamp / 86400); $year = floor($days / 365); $month = floor(($days - $year * 365) / 30); return $year."年".$month."ヶ月前"; }else{ //それ以前か $year = date("Y",time()) - date("Y",$timestamp); return $year."年前"; }; }; }; }; |