使用 PHP 添加两个类似格式的时间值


adding two time values of similar formats using php

我有两个时间值,如下所示

$time  = 06:58:00;
$time2 = 00:40:00;

我这样做是为了计算特定用户的约会和可用时间所以我用这种方式尝试

$max_date=abs(strtotime($time) + strtotime($time2));

但它返回 $max_date =2673452280任何建议请<</p>

div class="answers">

此代码示例将花费$time小时,并将$time2小时添加到其中

例如:时间 = 06:58:00,时间 2=00:40:00,结果 = 07:38:00

$time = "06:58:00";
$time2 = "00:40:00";
$secs = strtotime($time2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($time)+$secs);

使用此功能...

 function sum_the_time($time1, $time2) {
      $times = array($time1, $time2);
      $seconds = 0;
      foreach ($times as $time)
      {
        list($hour,$minute,$second) = explode(':', $time);
        $seconds += $hour*3600;
        $seconds += $minute*60;
        $seconds += $second;
      }
      $hours = floor($seconds/3600);
      $seconds -= $hours*3600;
      $minutes  = floor($seconds/60);
      $seconds -= $minutes*60;
      if($seconds < 9)
      {
      $seconds = "0".$seconds;
      }
      if($minutes < 9)
      {
      $minutes = "0".$minutes;
      }
        if($hours < 9)
      {
      $hours = "0".$hours;
      }
      return "{$hours}:{$minutes}:{$seconds}";
    }

strtotime 函数将 full date 作为参数,有效格式如下:http://www.php.net/manual/en/datetime.formats.php

您可以在在线PHP手册中看到该功能 http://php.net/manual/en/function.strtotime.php

如果您之前从数据库构建这些时间字符串,则可能需要将它们重建为如下所示的内容:

$time = "00:06:58";
$time2 = "40 minutes";
$timestamp = strtotime($time." +".$time2);
$endTime = date("d.m.Y H:i:s", $timestamp);

使用 php 添加两次的最简单方法是:

1) 将时间从 H:i:s(例如 08:15:40)格式转换为秒。
2)对第二次时间值执行相同的操作 参考:步骤1
3)添加转换后的值并存储PHP变量
4)现在将总计(以秒为单位)转换为H:i:s
它对我有用。

PHP 脚本:

$str_time ="08:04:40";
$str_time = preg_replace("/^(['d]{1,2})':(['d]{2})$/", "00:$1:$2", $str_time);
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$hrs_old_seconds = $hours * 3600 + $minutes * 60 + $seconds;
$str_time ="02:10:22";
$str_time = preg_replace("/^(['d]{1,2})':(['d]{2})$/", "00:$1:$2", $str_time);
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$hrs_toadd_seconds = $hours * 3600 + $minutes * 60 + $seconds;
$hrs_old_int1 = $hrs_old_seconds + $hrs_toadd_seconds;
echo $Total=gmdate("H:i:s", $hrs_old_int1);

结果= :10:15:02

Anudeep 的解决方案非常适合我的用例,但我也需要能够添加负数。下面是他的代码的略微编辑版本,用于获取并返回负时间字符串(例如"-01:01:01"):

public static function sum_the_times($time1, $time2)
{
    $times = array($time1, $time2);
    $seconds = 0;
    $negative = false;
    foreach ($times as $time) {
        list($hour,$minute,$second) = explode(':', $time);
        if(substr($hour,0,1) == '-'){
            $seconds -= substr($hour,1)*3600;
            $seconds -= $minute*60;
            $seconds -= $second;
        } else {
            $seconds += $hour*3600;
            $seconds += $minute*60;
            $seconds += $second;
        }
    }
    if (substr($seconds, 0, 1) == '-') {
        $negative = true;
        $seconds = ($seconds * -1);
    }
    $hours = floor($seconds/3600);
    $seconds -= $hours*3600;
    $minutes  = floor($seconds/60);
    $seconds -= $minutes*60;
    if ($seconds < 9) {
        $seconds = "0".$seconds;
    }
    if ($minutes < 9) {
        $minutes = "0".$minutes;
    }
    if ($hours < 9) {
        $hours = "0".$hours;
    }
    return ($negative ? "-" : "")."{$hours}:{$minutes}:{$seconds}";
}

你可以试试这个

$time = "04:00:00";
$time2 = "03:30:00";
$result = date("H:i:s",strtotime($time)+strtotime($time2));
echo $result;

它给出输出 07:30:00,但它在不同版本的操作系统中有时不起作用。如果你想得到时间的总和,那么你可以使用这个代码

<?php
   function CalculateTime($time1, $time2) {
      $time1 = date('H:i:s',strtotime($time1));
      $time2 = date('H:i:s',strtotime($time2));
      $times = array($time1, $time2);
      $seconds = 0;
      foreach ($times as $time)
      {
        list($hour,$minute,$second) = explode(':', $time);
        $seconds += $hour*3600;
        $seconds += $minute*60;
        $seconds += $second;
      }
      $hours = floor($seconds/3600);
      $seconds -= $hours*3600;
      $minutes  = floor($seconds/60);
      $seconds -= $minutes*60;
      if($seconds < 9)
      {
      $seconds = "0".$seconds;
      }
      if($minutes < 9)
      {
      $minutes = "0".$minutes;
      }
        if($hours < 9)
      {
      $hours = "0".$hours;
      }
      return "{$hours}:{$minutes}:{$seconds}";
    }
    $time1= '23:32:05';
    $time2 = '01:29';
    echo CalculateTime($time1,$time2);
?>

在第二个代码中,您可以以小时:分钟或小时:分钟:秒为单位发送时间。此代码接受这两种格式,因为它会自动转换时间

这是一个可以满足超过 24 小时且不使用 strtotime 的版本:

$time0 = "24:01:02";
$time1 = "01:02:03";
$matches0 = explode(':',$time0); // split up the string
$matches1 = explode(':',$time1);
$sec0 = $matches0[0]*60*60+$matches0[1]*60+$matches0[2];
$sec1 = $sec0+ $matches1[0]*3600+$matches1[1]*60+$matches1[2]; // get total seconds
$h = intval(($sec1)/3600);
$m = intval(($sec1-$h*3600)/60);
$s = $sec1-$h*3600-$m*60;
echo $str = str_pad($h, 2, '0', STR_PAD_LEFT).':'.str_pad($m, 2, '0', STR_PAD_LEFT).':'.str_pad($s, 2, '0', STR_PAD_LEFT);