【问题标题】:Mathematical Operation PHP With time数学运算 PHP 随着时间
【发布时间】:2014-01-31 01:13:37
【问题描述】:

我正在准备用 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)) ;

感谢您的帮助;)

【问题讨论】:

  • Lo mejor es usar DateTime() and DateTime::diff() en el manual puedes encontrar los ejemplos tanto procedural como orientado a objetos, DateTime::diff() te devuelve la diferenca entre dos fechas en el formato que le especifiques @ 987654323@

标签: php mysql date datetime date-math


【解决方案1】:

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");

【讨论】:

【解决方案2】:

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 或更高版本)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    相关资源
    最近更新 更多