12

重複の可能性:
PHPを使用して2つの日付の差を計算する方法は?

ここで私はその日付で2回言及します

2008-12-13 10:42:00

2010-10-20 08:10:00

(h:m:s)形式で合計時間差を取得したい

4

3 に答える 3

33

PHP 5.3.x 以降を使用している、または使用できる場合は、その DateTime オブジェクト機能を使用できます。

$date_a = new DateTime('2010-10-20 08:10:00');
$date_b = new DateTime('2008-12-13 10:42:00');

$interval = date_diff($date_a,$date_b);

echo $interval->format('%h:%i:%s');

この形式はさまざまな方法で操作できます。DateTime オブジェクトに日付が含まれていると、通常の演算子による比較など、さまざまな機能を利用できます。詳細については、マニュアルを参照してください: http://us3.php.net/manual/en/datetime.diff.php

于 2012-05-22T06:23:12.737 に答える
16

私が使用しているもの:

$seconds = strtotime("2010-10-20 08:10:00") - strtotime("2008-12-13 10:42:00");

$days    = floor($seconds / 86400);
$hours   = floor(($seconds - ($days * 86400)) / 3600);
$minutes = floor(($seconds - ($days * 86400) - ($hours * 3600))/60);
$seconds = floor(($seconds - ($days * 86400) - ($hours * 3600) - ($minutes*60)));

今すぐフォーマットできます

于 2012-05-22T06:29:07.790 に答える
4

strtotime関数を使用して、時間を整数に変換し、それらを減算することができます。

$time1 = strtotime("2008-12-13 10:42:00");
$time2 = strtotime("2010-10-20 08:10:00");

$diff = $time2-$time1;
// the difference in int. then you can divide by 60,60,24 and 
// so on to get the h:m:s out of it
于 2012-05-22T06:16:22.770 に答える