[php] PHP에서 두 날짜 사이의 시간 계산

두 날짜의 시간 차이를 어떻게 계산합니까?

예를 들면 :

day1=2006-04-12 12:30:00
day2=2006-04-14 11:30:00

이 경우 결과는 47 시간이어야합니다.



답변

새로운 PHP-버전이라는 새로운 클래스를 제공 DateTime, DateInterval, DateTimeZoneDatePeriod. 이 수업의 멋진 점은 다양한 시간대, 윤년, 윤초, 여름철 등을 고려한다는 것입니다. 게다가 사용하기가 매우 쉽습니다. 이 개체의 도움으로 원하는 것은 다음과 같습니다.

// Create two new DateTime-objects...
$date1 = new DateTime('2006-04-12T12:30:00');
$date2 = new DateTime('2006-04-14T11:30:00');

// The diff-methods returns a new DateInterval-object...
$diff = $date2->diff($date1);

// Call the format method on the DateInterval-object
echo $diff->format('%a Day and %h hours');

반환되는 DateInterval 개체는 format. 몇 시간 만에 결과를 얻으려면 다음과 같이 할 수 있습니다.

$date1 = new DateTime('2006-04-12T12:30:00');
$date2 = new DateTime('2006-04-14T11:30:00');

$diff = $date2->diff($date1);

$hours = $diff->h;
$hours = $hours + ($diff->days*24);

echo $hours;

문서 링크는 다음과 같습니다.

이 모든 클래스는 날짜를 조작하는 절차 적 / 기능적 방법도 제공합니다. 따라서 개요를 살펴보십시오 : http://php.net/manual/book.datetime.php


답변

$t1 = strtotime( '2006-04-14 11:30:00' );
$t2 = strtotime( '2006-04-12 12:30:00' );
$diff = $t1 - $t2;
$hours = $diff / ( 60 * 60 );


답변

UTC 또는 GMT 시간대 를 DatePeriod사용할 때 다른 방법을 제공 합니다.

시간 계산 https://3v4l.org/Mu3HD

$start = new \DateTime('2006-04-12T12:30:00');
$end = new \DateTime('2006-04-14T11:30:00');

//determine what interval should be used - can change to weeks, months, etc
$interval = new \DateInterval('PT1H');

//create periods every hour between the two dates
$periods = new \DatePeriod($start, $interval, $end);

//count the number of objects within the periods
$hours = iterator_count($periods);
echo $hours . ' hours'; 

//difference between Unix Epoch
$diff = $end->getTimestamp() - $start->getTimestamp();
$hours = $diff / ( 60 * 60 );
echo $hours . ' hours (60 * 60)';

//difference between days
$diff = $end->diff($start);
$hours = $diff->h + ($diff->days * 24);
echo $hours . ' hours (days * 24)';

결과

47 hours (iterator_count)
47 hours (60 * 60)
47 hours (days * 24)

일광 절약 시간 계산 https://3v4l.org/QBQUB

그 알려드립니다 DatePeriod제외에게 DST에 대한 시간 만 때 DST가 종료 한 시간을 추가하지 않습니다. 따라서 사용은 원하는 결과와 날짜 범위에 따라 달라집니다.

현재 버그 보고서보기

//set timezone to UTC to disregard daylight savings
date_default_timezone_set('America/New_York');

$interval = new \DateInterval('PT1H');

//DST starts Apr. 2nd 02:00 and moves to 03:00
$start = new \DateTime('2006-04-01T12:00:00');
$end = new \DateTime('2006-04-02T12:00:00');

$periods = new \DatePeriod($start, $interval, $end);
$hours = iterator_count($periods);
echo $hours . ' hours';

//DST ends Oct. 29th 02:00 and moves to 01:00
$start = new \DateTime('2006-10-28T12:00:00');
$end = new \DateTime('2006-10-29T12:00:00');

$periods = new \DatePeriod($start, $interval, $end);
$hours = iterator_count($periods);
echo $hours . ' hours';

결과

#2006-04-01 12:00 EST to 2006-04-02 12:00 EDT
23 hours (iterator_count)
//23 hours (60 * 60)
//24 hours (days * 24)

#2006-10-28 12:00 EDT to 2006-10-29 12:00 EST
24 hours (iterator_count)
//25 hours (60 * 60)
//24 hours (days * 24)

#2006-01-01 12:00 EST to 2007-01-01 12:00 EST
8759 hours (iterator_count)
//8760 hours (60 * 60)
//8760 hours (days * 24)

//------

#2006-04-01 12:00 UTC to 2006-04-02 12:00 UTC
24 hours (iterator_count)
//24 hours (60 * 60)
//24 hours (days * 24)

#2006-10-28 12:00 UTC to 2006-10-29 12:00 UTC
24 hours (iterator_count)
//24 hours (60 * 60)
//24 hours (days * 24)

#2006-01-01 12:00 UTC to 2007-01-01 12:00 UTC
8760 hours (iterator_count)
//8760 hours (60 * 60)
//8760 hours (days * 24)


답변

당신의 대답은 :

round((strtotime($day2) - strtotime($day1))/(60*60))


답변

일광 절약 시간이 변경 되더라도 두 날짜 (날짜 시간) 사이의 정확한 시간을 얻는 가장 쉬운 방법은 Unix 타임 스탬프의 차이를 사용하는 것입니다. Unix 타임 스탬프는 1970-01-01T00 : 00 : 00 UTC 이후 경과 된 초로 윤초를 무시합니다 (이 정밀도가 필요하지 않고 윤초를 고려하기가 매우 어렵 기 때문에 괜찮습니다).

선택적 시간대 정보가있는 datetime 문자열을 Unix 타임 스탬프로 변환하는 가장 유연한 방법은 DateTime 객체 (선택적으로 생성자의 두 번째 인수로 DateTimeZone 사용 )를 생성 한 다음 getTimestamp 메서드 를 호출하는 입니다.

$str1 = '2006-04-12 12:30:00';
$str2 = '2006-04-14 11:30:00';
$tz1 = new DateTimeZone('Pacific/Apia');
$tz2 = $tz1;
$d1 = new DateTime($str1, $tz1); // tz is optional,
$d2 = new DateTime($str2, $tz2); // and ignored if str contains tz offset
$delta_h = ($d2->getTimestamp() - $d1->getTimestamp()) / 3600;
if ($rounded_result) {
   $delta_h = round ($delta_h);
} else if ($truncated_result) {
   $delta_h = intval($delta_h);
}
echo "Δh: $delta_h\n";


답변

//Calculate number of hours between pass and now
$dayinpass = "2013-06-23 05:09:12";
$today = time();
$dayinpass= strtotime($dayinpass);
echo round(abs($today-$dayinpass)/60/60);


답변

$day1 = "2006-04-12 12:30:00"
$day1 = strtotime($day1);
$day2 = "2006-04-14 11:30:00"
$day2 = strtotime($day2);

$diffHours = round(($day2 - $day1) / 3600);

strtotime () 함수가이 날짜 형식을 받아들이는 것 같아요.