분류없음2012.07.17 01:03
  

http://jobdahan.net/index.php?mid=language_php&document_srl=1232068
http://code.p-ark.co.kr/92


날짜 예제

복사해서 php파일에 넣어 결과를 확인하세요

<?

$ctime = date('Y-m-d H:i:s');
//$rtime = "1942-01-01 00:00:01";
//$rtime = "1941-05-19 09:00:00";
//$rtime = "20090606";
//$rtime = "20090606093924";
//$rtime = "2009-06-06 09:39:24";
//2012-07-16 22:16:06


function datetimediff($rtime, $ctime = null, $option = null){
      if ($ctime) $cur_time = strtotime($ctime);
      else $cur_time = time();
      $ref_time = strtotime($rtime);

      $cur_date = floor($cur_time / 86400);
      $ref_date = floor($ref_time / 86400);

      $datetimediff = $cur_time - $ref_time;
      $datedist = $cur_date - $ref_date;
      $datediff = floor($datetimediff / 86400);
      $weekdiff = floor($datediff / 7);
      $timediff = $datetimediff % 86400;

      $hour = floor($timediff / 3600);
      $min = floor($timediff % 3600 / 60);
      $sec = floor($timediff % 3600 % 60);

      $result = "";
      if ($datedist>34) {
            $result = date("Y년 n월 j일", $ref_time);
      } else if ($weekdiff>0) {
            $result = $weekdiff . "주 전";
      } else {
            if ($datediff>0) {
                  $result = $datedist . "일 전";
            } else if ($timediff<=0) {
                  $result = "1초 전";
            } else {
                  if ($hour) $result = $hour . "시간";
                  else if ($min) $result = $min . "분";
                  else $result = $sec . "초";
                  if ($result) $result .= " 전";
            }
      }
      if ($option=='ALL') {
            $result = "";
            if ($datediff) $result .= ($result?" ":"") . $datediff."일";
            if ($hour) $result .= ($result?" ":"") . $hour."시간";
            if ($min) $result .= ($result?" ":"") . $min ."분";
            if ($sec) $result .= ($result?" ":"") . $sec . "초";
            $result .= " 전";
      }
      return $result;
}

   
$ctime = date('Y-m-d H:i:s');
$temp_y2 = substr($ctime,0,4);   
$temp_m2 = substr($ctime,5,2);   
$temp_d2 = substr($ctime,8,2);   

$temp_f1 = $rs[ufeedDate][$i];
$temp_y1 = substr($temp_f1,0,4);   
$temp_m1 = substr($temp_f1,5,2);   
$temp_d1 = substr($temp_f1,8,2);   


echo $temp_y1."<br>";
echo $temp_m1."<br>";
echo $temp_d1."<br>";

echo $temp_y2."<br>";
echo $temp_m2."<br>";
echo $temp_d2."<br>";



$start_date = mktime(00, 00, 00, $temp_m1, $temp_d1, $temp_y1); //시작점
echo $start_date;


$finish_date = mktime(00, 00, 00,$temp_m2 , $temp_d2, $temp_y2); //끝점
echo $finish_date;
echo "(".(($start_date-$finish_date)/60/60/24)."일)"."<br>"; //날짜 계산 초로 반환됨

$temp_day_cal =(($start_date-$finish_date)/60/60/24);
$temp_time_cal = $temp_day_cal * 24;

$rtime = date('Y-m-d H:i:s', strtotime($temp_time_cal." hours -51 minutes -24 seconds"));
$xtime = date('Y-m-d H:i:s', strtotime("+1 hours 12 minutes 35 seconds"));

echo "$ctime<br>\n";
echo "$rtime<br>\n";
echo "$xtime<br>\n";

echo datetimediff($rtime) . "<br>\n";
$temp_final_date = datetimediff($rtime);
echo datetimediff($rtime, null, "ALL") . "<br>\n";
echo datetimediff($rtime, $xtime, "ALL") . "<br>\n";


$time = time();
echo date("2012-07-10") - date("2012-07-12") ."<br>";
echo date("Y-m-d",strtotime($temp123, $time))." 하루 전(어제)"."<br>";
echo date("Y-m-d",strtotime("-1 day", $time))." 하루 전(어제)"."<br>";
echo date("Y-m-d",strtotime("now", $time))." 현재"."<br>";
echo date("Y-m-d",strtotime("+1 day", $time))." 하루 후(내일)"."<br>";
echo date("Y-m-d",strtotime("+1 week", $time))." 일주일 후"."<br>";
echo date("Y-m-d",strtotime("-1 month", $time))." 한달 전"."<br>";
echo date("Y-m-d",strtotime("+1 month", $time))." 다음달"."<br>";
echo date("Y-m-d",strtotime("+6 month", $time))." 6달후"."<br>";
echo date("Y-m-d",strtotime("+12 month", $time))." 12달후"."<br>";
echo date("Y-m-d",strtotime("next Thursday", $time))." 다음주 목요일"."<br>";
echo date("Y-m-d",strtotime("last Monday", $time))." 지난 월요일"."<br>";
echo date("Y-m-d",strtotime("10 September 2000", $time))." 2000년 9월 10일 "."<br>";
echo strtotime("+5 minutes")." 현재 시간보다 5분 후"."<br>";

?>

Posted by 예배하는 프로그래머