0

私はphpで数学演算をしようとしています.2つの変数があります.

$a2 = $info["Hora_final"]; //END TIME Example-> 15:30:00
$a1 = $info["Hora_inicial"];//FIRST TIME Example-> 10:00:20

But i want this operation (-) minus.
$res = $a2 - $a1 ;
echo $res;
//output  5,30 Hours difference for example.

私はこの機能を試してみましたが、私が望むようなものではありません。

 $a2 = $info["Hora_final"];
 $a1 = $info["Hora_inicial"];

$h2h = date('H', strtotime($a2));
$h2m = date('i', strtotime($a2));
$h2s = date('s', strtotime($a2));
$hora2 =$h2h." hour ". $h2m ." min ".$h2s ." second";

$horas_sumadas= $a1." - ". $hora2;
$text=date('H:i:s', strtotime($horas_sumadas)) ;

助けてくれてありがとう;)

4

2 に答える 2

2

DateTime()DateInterval()は、あなたが探しているものです:

$date1 = new DateTime($info["Hora_final"]);
$date2 = new DateTime($info["Hora_inicial"]);
$diff = $date1->diff($date2);
echo $diff->format("%h hours, %i minutes");
于 2014-01-31T01:20:14.917 に答える
1

これには、PHP の DateTime クラスが最適です。http://www.php.net/manual/en/class.datetime.php

2 つの DateTime オブジェクトを作成し、次のdiffメソッドを使用します: http://www.php.net/manual/en/datetime.diff.php

(PHP 5.2 以降が必要です)

于 2014-01-31T01:16:12.273 に答える