글 목록을 보여줄 때 현재 시간으로 보여주는 경우도 많지만 많은 SNS에서 지금으로부터 얼마 전에 써진 글인지 표시해주는 경우도 많습니다.
이런 경우에 사용할 수 있는 함수 예제입니다.
function readableTime($datetime) {
$sec = time() – strtotime($datetime);
if($sec < 60) return $time . ‘초’;
$min = $sec / 60;
if($min < 60) return intval($min) . ‘분’;
$hour = $min / 60;
if($hour < 24) return intval($hour) . ‘시간’;
$day = $hour / 24;
if($day < 7) return intval($day) . ‘일’;
$week = $day / 7;
if($time < 5) return intval($week) . ‘주’;
$month = $day / 30;
if($month < 24) return intval($month) . ‘개월’;
$year = $day / 365;
return intval($year) . ‘년’;
}
More from my site
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Leave a Comment