"함수 timeago()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
6번째 줄: 6번째 줄:
==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[category: JavaScript]]
<source lang='JavaScript'>
<syntaxhighlight lang='JavaScript'>
function timeago(date) {
function timeago(date) {
var t = new Date(date);
var t = new Date(date);
15번째 줄: 15번째 줄:
return "방금";
return "방금";
}
}
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
<source lang='php'>
<syntaxhighlight lang='php'>
function timeago($str) {
function timeago($str) {
$t = strtotime($str);
$t = strtotime($str);
28번째 줄: 28번째 줄:
return '방금';
return '방금';
}
}
</source>
</syntaxhighlight>
* [[Carbon 클래스]] 사용
* [[Carbon 클래스]] 사용
<source lang='php'>
<syntaxhighlight lang='php'>
echo \Carbon\Carbon::createFromTimeStamp(strtotime('2016-09-17 12:00'))->diffForHumans();
echo \Carbon\Carbon::createFromTimeStamp(strtotime('2016-09-17 12:00'))->diffForHumans();
# 1 hour ago
# 1 hour ago
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[함수 bbstime()]]
*[[함수 bbstime()]]
*[[3600, 86400]]
*[[3600, 86400]]

2020년 11월 2일 (월) 02:36 기준 최신판

함수 agotime()
함수 timeago()
함수 snstime()
함수 relative_time()

1 JavaScript[ | ]

function timeago(date) {
	var t = new Date(date);
	var seconds = Math.floor((new Date() - t.getTime()) / 1000);
	if(seconds>86400) return t.toISOString().substring(0, 10);
	if(seconds>3600) return Math.floor(seconds/3600)+"시간전";
	if(seconds>60) return Math.floor(seconds/60)+"분전";
	return "방금";
}

2 PHP[ | ]

function timeago($str) {
	$t = strtotime($str);
	$seconds = time() - $t;
	if( $seconds>86400 ) return date('Y-m-d', $t);
	if( $seconds>3600 ) return ceil($seconds/3600).'시간전';
	if( $seconds>60 ) return ceil($seconds/60).'분전';
	return '방금';
}
echo \Carbon\Carbon::createFromTimeStamp(strtotime('2016-09-17 12:00'))->diffForHumans();
# 1 hour ago

3 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}