문제

이 질문은 이미 여기에 답이 있습니다.

PHP의 날짜 차이를 계산하는 빠른 방법이 있습니까? 예를 들어:

$date1 = '2009-11-12 12:09:08';
$date2 = '2009-12-01 08:20:11';

그런 다음 계산, $ date2 inmus $ date1

php.net 문서를 읽었지만 운이 없습니다. 빠른 방법이 있습니까?

도움이 되었습니까?

해결책

strtotime은 날짜 문자열을 Unix 타임 스탬프로 변환합니다. (유닉스 시대 이후 몇 초.

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);

$seconds_diff = $ts2 - $ts1;

다른 팁

사용하는 것이 좋습니다 날짜-> diff 아래 예에서와 같이 기능 :

   $dStart = new DateTime('2012-07-26');
   $dEnd  = new DateTime('2012-08-26');
   $dDiff = $dStart->diff($dEnd);
   echo $dDiff->format('%r%a'); // use for point out relation: smaller/greater

보다 http://www.php.net/manual/en/datetime.diff.php

아래 코드는 두 날짜의 차이를 꺼내어 일수의 출력을 제공합니다.

$str = "Jul 02 2013";
$str = strtotime(date("M d Y ")) - (strtotime($str));
echo floor($str/3600/24);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top