计算负时和正时

时间:2016-12-12 11:19:08

标签: php datetime addition subtraction hour

我的持续时间为%RH:i:s格式(如+00:00:00或-00:00:00),我想添加或减去它们(带负数)而不是24小时

  

前:

     

1 /(09:30:15 +( - 10:00:00))= - 01:30:15。

     

2 / 22:00:00 + 03:00:00 = 25:00:00(不是01:00:00 + 1day)

有什么想法吗?提前谢谢!

修改

终于做到了。不知道它是否是真正正确的方法,但效果非常好。在论坛上发现的另一个功能的帮助下找到它。

function calc_hours($hour1,$hour2){

    $si1 = $hour1[0]; $si2 = $hour2[0]; 

    $hour1 = substr($hour1,1,8);
    $hour2 = substr($hour2,1,8);        

    $secondes1=intval($si1.heure_to_secondes($hour1));
    $secondes2=intval($si2.heure_to_secondes($hour2));

    $somme=intval($secondes1+$secondes2);

    //transfo en h:i:s
    $s= ($somme % 60); 
    $m1=   (($somme-$s) / 60);
    $m= ($m1 % 60);
    $h= (($m1-$m) / 60); 

    if($somme > 0) { $sif = '+'; }
    $resultat=sprintf("%02d", $h).":".sprintf("%02d", abs($m)).":".sprintf("%02d", abs($s))."";
    return  $sif.$resultat;
}

function heure_to_secondes($heure){
    $array_heure=explode(":",$heure);
    $secondes=3600*$array_heure[0]+60*$array_heure[1]+$array_heure[2];
    return $secondes;
}

称之为:calc_hours(' +27:45:16',' -02:35:12');

1 个答案:

答案 0 :(得分:0)

终于做到了。不知道这是否是真正正确的方法,但它的效果非常好。在论坛上发现的另一个功能的帮助下找到它。

function calc_hours($hour1,$hour2){

    $si1 = $hour1[0]; $si2 = $hour2[0]; 

    $hour1 = substr($hour1,1,8);
    $hour2 = substr($hour2,1,8);        

    $secondes1=intval($si1.heure_to_secondes($hour1));
    $secondes2=intval($si2.heure_to_secondes($hour2));

    $somme=intval($secondes1+$secondes2);

    //transfo en h:i:s
    $s= ($somme % 60); 
    $m1=   (($somme-$s) / 60);
    $m= ($m1 % 60);
    $h= (($m1-$m) / 60); 

    if($somme > 0) { $sif = '+'; }
    $resultat=sprintf("%02d", $h).":".sprintf("%02d", abs($m)).":".sprintf("%02d", abs($s))."";
    return  $sif.$resultat;
}

function heure_to_secondes($heure){
    $array_heure=explode(":",$heure);
    $secondes=3600*$array_heure[0]+60*$array_heure[1]+$array_heure[2];
    return $secondes;
}

称之为:calc_hours('+ 27:45:16',' - 02:35:12');