source

날짜 차이(일별 php)

bestscript 2022. 11. 13. 19:35

날짜 차이(일별 php)

날짜 차이를 php로 계산하는 빠른 방법이 있나요?예를 들어 다음과 같습니다.

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

그런 다음 $date2에서 $date1을 뺀 값을 계산합니다.

php.net의 매뉴얼을 읽었는데, 잘 되지 않습니다.빨리 할 수 있는 방법이 없을까요?

다음과 같이 date->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 를 참조해 주세요.

strtotime은 날짜 문자열을 UNIX 타임스탬프로 변환합니다.(unix epoch 이후 초수).

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

$seconds_diff = $ts2 - $ts1;

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

$str = "Jul 02 2013";
$str = strtotime(date("M d Y ")) - (strtotime($str));
echo floor($str/3600/24);

언급URL : https://stackoverflow.com/questions/1940338/date-difference-in-php-on-days