如何减去两个日期并以分钟为单位获取差异

时间:2019-06-06 23:51:16

标签: php date

所以我有两个采用小时格式的日期。我想将它们相减并在几分钟内得到差异。

    NAME                TYPE           CLUSTER-IP      EXTERNAL-IP       PORT(S)          AGE
mysql               ClusterIP      10.47.254.97    <none>            3306/TCP         25m
petclinic-service   LoadBalancer   10.47.243.216   104.196.116.129   8081:31781/TCP   25m

3 个答案:

答案 0 :(得分:1)

另一种方法:

signature ERRORMSG = sig val anyErrors : bool ref val fileName : string ref val lineNum : int ref val linePos : int list ref val sourceStream : TextIO.instream ref val error : int -> string -> unit exception Error val impossible : string -> 'a (* raises Error *) val reset : unit -> unit end structure ErrorMsg : ERRORMSG = struct val anyErrors = ref false val fileName = ref "" val lineNum = ref 1 val linePos = ref [1] val sourceStream = ref TextIO.stdIn fun reset() = (anyErrors:=false; fileName:=""; lineNum:=1; linePos:=[1]; sourceStream:=TextIO.stdIn) exception Error fun error pos (msg:string) = let fun look(a::rest,n) = if a<pos then app print [":", Int.toString n, ".", Int.toString (pos-a)] else look(rest,n-1) | look _ = print "0.0" in anyErrors := true; print (!fileName); look(!linePos,!lineNum); print ":"; print msg; print "\n" end fun impossible msg = (app print ["Error: Compiler bug: ",msg,"\n"]; TextIO.flushOut TextIO.stdOut; raise Error) end

第三个参数echo date_diff(new DateTime('01:47'), new DateTime('01:50'), true)->i;用于强制给出正结果,而true则用于获取date_diff返回的DateInterval的分钟数。

答案 1 :(得分:0)

$time1 = "23:58";
  $time2 = "01:00";
  $time1 = explode(':',$time1);
  $time2 = explode(':',$time2);
  $hours1 = $time1[0];
  $hours2 = $time2[0];
  $mins1 = $time1[1];
  $mins2 = $time2[1];
  $hours = $hours2 - $hours1;
  $mins = 0;
  if($hours < 0)
  {
    $hours = 24 + $hours;
  }
  if($mins2 >= $mins1) {
        $mins = $mins2 - $mins1;
    }
    else {
      $mins = ($mins2 + 60) - $mins1;
      $hours--;
    }
    if($mins < 9)
    {
      $mins = str_pad($mins, 2, '0', STR_PAD_LEFT);
    }
    if($hours < 9)
    {
      $hours =str_pad($hours, 2, '0', STR_PAD_LEFT);
    }
echo $hours*60+$mins;

另一种方法

$to_time = strtotime("01:47:00");
$from_time = strtotime(" 01:55:00");
echo round(abs($to_time - $from_time) / 60). " minute";

答案 2 :(得分:0)

最简单的解决方案可能是使用strtotime并除以60:

$time = strtotime('01:47');
$time2 = strtotime('01:50');

$finalresult = ($time2 - $time) / 60;