짧은 코드 긴 내용 - 초 간단 날짜 차이
스쿨 길드 정보에 올렸던 "재미있을까 재미없을까 200바이트 이하의 장난성 코드들…."
$a를 비웠기에 오늘 '20110828'과 대상 '20110912' 날짜 간 차이를 구합니다.
실행 결과 '15일 남음' 출력됩니다. 추석까지 15일 남았군요.
sscanf() - 포맷에 따라 문자열 해석합니다.
http://php.net/manual/kr/function.sscanf.php [한글 구 버전]
http://php.net/manual/en/function.sscanf.php [영문 신 버전]
date() - 다양한 포맷으로 날짜를 구할 수 있습니다.
http://php.net/manual/kr/function.date.php
http://php.net/manual/en/function.date.php
mktime() - 날짜와 시간을 이용해 timestamp를 구할 수 있습니다.
timestamp는 1970년 1월 1일부터의 초(second)입니다.
http://php.net/manual/en/function.mktime.php
* 각 값에 숫자를 더하거나 빼면 날짜나 시간을 간편하게 구할 수 있습니다.
abs() - 절대값을 구합니다.
http://php.net/manual/kr/function.abs.php
http://php.net/manual/en/function.abs.php
하루는 60(초) * 60(분) * 24(시간) = 86,400초입니다.
그래서 두 날짜의 timestamp의 차를 86,400으로 나누면
일수를 구할 수 있습니다.
짧지만 지났는지, 오늘인지, 오늘인지까지 신경 쓰는 깔끔한 코드입니다.
<?php
$a='';$b='20110912';sscanf(($a?$a:date('Ymd')).$b,'%4d%2d%2d%4d%2d%2d',$c,$d,$e,$f,$g,$h);echo(!($i=(mktime(0,0,0,$g,$h,$f)-mktime(0,0,0,$d,$e,$c))/86400)?'오늘':abs($i).'일 '.($i<0?'지남':'남음'));
?>
$a='';$b='20110912';sscanf(($a?$a:date('Ymd')).$b,'%4d%2d%2d%4d%2d%2d',$c,$d,$e,$f,$g,$h);echo(!($i=(mktime(0,0,0,$g,$h,$f)-mktime(0,0,0,$d,$e,$c))/86400)?'오늘':abs($i).'일 '.($i<0?'지남':'남음'));
?>
$a를 비웠기에 오늘 '20110828'과 대상 '20110912' 날짜 간 차이를 구합니다.
실행 결과 '15일 남음' 출력됩니다. 추석까지 15일 남았군요.
sscanf() - 포맷에 따라 문자열 해석합니다.
http://php.net/manual/kr/function.sscanf.php [한글 구 버전]
http://php.net/manual/en/function.sscanf.php [영문 신 버전]
date() - 다양한 포맷으로 날짜를 구할 수 있습니다.
http://php.net/manual/kr/function.date.php
http://php.net/manual/en/function.date.php
mktime() - 날짜와 시간을 이용해 timestamp를 구할 수 있습니다.
timestamp는 1970년 1월 1일부터의 초(second)입니다.
http://php.net/manual/en/function.mktime.php
* 각 값에 숫자를 더하거나 빼면 날짜나 시간을 간편하게 구할 수 있습니다.
abs() - 절대값을 구합니다.
http://php.net/manual/kr/function.abs.php
http://php.net/manual/en/function.abs.php
하루는 60(초) * 60(분) * 24(시간) = 86,400초입니다.
그래서 두 날짜의 timestamp의 차를 86,400으로 나누면
일수를 구할 수 있습니다.
짧지만 지났는지, 오늘인지, 오늘인지까지 신경 쓰는 깔끔한 코드입니다.
함께 나눈 이야기
댓글댓글 남기기