代码之家  ›  专栏  ›  技术社区  ›  tranthaihoang

时间减去PHP上的时间[重复]

  •  1
  • tranthaihoang  · 技术社区  · 7 年前

    $a = "17:20:00"; 我想做

      if($a > "13:00:00"){
        $c = $a - "01:00:00";
      }
    

    我想得到 $c = "16:20:00" 如何做到这一点? 非常感谢。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Andreas    7 年前

    $a是一个字符串,不能这样比较。
    使用 strtotime 将其设为UNIX时间(整数)并减去3600秒。
    然后使用 date 转换回字符串。

    $a = strtotime("17:20:00");
        if($a > strtotime("13:00:00")){
            $c = $a - 3600;
    }
    
    Echo date("H:i", $c);
    

    https://3v4l.org/fOggP

        2
  •  -1
  •   AddWeb Solution Pvt Ltd    7 年前

    你应该试试这个:

    if(strtotime($a) > strtotime("13:00:00")){
        $c = date( "h:i:s", strtotime($a) - strtotime("01:00:00")); 
      }