프로그래밍/php
날짜 or 시간
verdana
2018. 2. 27. 11:18
## 기간 일 구하기
echo intval((strtotime(종료일) - strtotime(시작일)) / 86400);
## 날짜 몇분전 으로 표시
// $date1:기준시간 timestamp // $date2:계산할시간 timestamp function getGlobalDate($date1, $date2) { $dtDiff = $date1 - $date2; if($dtDiff < 60) $rs = intval($dtDiff) . "/sec"; else if($dtDiff < 60*60) $rs = intval($dtDiff / (60)) . "/min"; else if($dtDiff < 60*60*24) $rs = intval($dtDiff / (60*60)) . "/hour"; else if($dtDiff < 60*60*24*7) $rs = intval($dtDiff / (60*60*24)) . "/day"; else if($dtDiff < 60*60*24*30) $rs = intval($dtDiff / (60*60*24*7)) . "/week"; else if($dtDiff < 60*60*24*365) $rs = intval($dtDiff / (60*60*24*30)) . "/month"; else $rs = intval($dtDiff / (60*60*24*365)) . "/year"; return $rs; }
## 남은시간 계산
/* 설 명 해당시간의 남은시간 계산함수 사용예 diff_time(timestamp) 리턴값 배열 */ function diff_time( $timestamp ) { $date1 = $timestamp; $date2 = time(); $total_secs = abs($date1-$date2); $diff_in_days = floor( $total_secs / 86400 ); $rest_hours = $total_secs % 86400; $diff_in_hours = floor( $rest_hours / 3600 ); $rest_mins = $rest_hours % 3600; $diff_in_mins = floor( $rest_mins / 60 ); $diff_in_secs = floor( $rest_mins % 60 ); $time_diff["days"] = (int)($diff_in_days); $time_diff["hours"] = $diff_in_hours; $time_diff["mins"] = $diff_in_mins; $time_diff["secs"] = $diff_in_secs; return $time_diff; }
## 한시간 전 날짜와 시간 구하기
echo date("Y-m-d H:i:s", mktime(date("H")-1, date("i"), date("s"), date("m"), date("d"), date("Y"))); 15일전 날짜 구하기 $date = date('Y-m-d H:i:s', strtotime('-15 day')); 타임스템프 변환 $sch_end_date = '2017-01-30 23:59:59'; $timestamp = strtotime($sch_end_date); echo date('Y-m-d H:i:s', $timestamp); exit; $time = time(); date("Y-m-d",strtotime("-1 day", $time)); // 하루 전(어제) date("Y-m-d",strtotime("-1 day", $time)); // 하루 전(어제) date("Y-m-d",strtotime("now", $time)); // 현재 date("Y-m-d",strtotime("+1 day", $time)); // 하루 후(내일) date("Y-m-d",strtotime("+1 week", $time)); // 일주일 후 date("Y-m-d",strtotime("-1 month", $time)); // 한달 전 date("Y-m-d",strtotime("+1 month", $time)); // 다음달 date("Y-m-d",strtotime("+6 month", $time)); // 6달후 date("Y-m-d",strtotime("+12 month", $time)); // 12달후 date("Y-m-d",strtotime("next Thursday", $time)); // 다음주 목요일 date("Y-m-d",strtotime("last Monday", $time)); // 지난 월요일 date("Y-m-d",strtotime("10 September 2000", $time)); // 2000년 9월 10일 date("Y-m-d", strtotime('first day of')) // 해당월의 1일 date("Y-m-d:i:s", strtotime("now", $time)); //현재 시간 date("Y-m-d:i:s", strtotime("+5 minutes", $time)); //현재 시간에서 5분 후
## 한주의 시작일과 마지막일 구하기
$today = date("Ymd"); $week_day = date("w", mktime(0, 0, 0, substr($today, 4, 2), substr($today, 6, 2), substr($today, 0, 4)) ); $week_start = date("Ymd", mktime(0, 0, 0, substr($today, 4, 2), substr($today, 6, 2)-$week_day, substr($today, 0, 4)) ); $week_day2 = 6 - $week_day; $week_end = date("Ymd", mktime(0, 0, 0, substr($today, 4, 2), substr($today, 6, 2)+$week_day2, substr($today, 0, 4)) ); echo $week_start ."~". $week_end;