Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:Time/HH:Minute:Seconds转换为秒_Php - Fatal编程技术网

PHP:Time/HH:Minute:Seconds转换为秒

PHP:Time/HH:Minute:Seconds转换为秒,php,Php,我有一个代码,它将减去总持续时间和总时间,然后计算结果将转换为秒 假设在我的中,总持续时间为“02:00:00” 那么对于总时间为“01:30:00” 为了计算 02:00:00 - 01:30:00 = 00:30:00 然后对于结果,“00:30:00”将转换为秒,结果为“1800” 我如何转换它 感谢您的帮助…使用功能。它返回UNIX时间戳(自1970年1月1日00:00:00以来的秒数)。如果您将小时格式HH:MM:SS传递给它,您可以轻松地进行计算 $to = strtotime('

我有一个代码,它将减去
总持续时间
总时间
,然后计算结果将转换为秒

假设在我的
中,总持续时间为“02:00:00”
那么对于
总时间
为“01:30:00”

为了计算

02:00:00 - 01:30:00 = 00:30:00
然后对于结果,“00:30:00”将转换为秒,结果为“1800”

我如何转换它

感谢您的帮助…

使用功能。它返回UNIX时间戳(自1970年1月1日00:00:00以来的秒数)。如果您将小时格式
HH:MM:SS
传递给它,您可以轻松地进行计算

$to = strtotime('02:00:00');
$from = strtotime('01:30:00');

$seconds = $to - $from; // outputs 30
您假设格式为
minutes:seconds:milisonds
,您希望在本例中接收30seconds。实际上,输出是30分钟。毫秒用点分隔。
您的工作时间应该如下所示:

$to = strtotime('00:02:00');
$from = strtotime('00:01:30');
你能试试这个吗

    $start = '01:30:00';
    $end  = '02:00:00';
    $workingHours = (strtotime($end) - strtotime($start));
    $res= date("i", $workingHours); 
    echo "DIFF: ". $res; //OP 30 Minutes

    echo  $resFull= date("H:i:s", $workingHours); //OP 00:30:00 

如果使用格式
HH:MM:SS
,则可以通过下一个代码将其转换为秒

$timestr = "00:30:00";
$temp = explode(":", $timestr);
if ($temp && is_array($temp) && count($temp) == 3) {
  $time = intval($temp[0]) * 3600 + intval($temp[1]) * 60 + intval($temp[1]);
} else {
  $time = null;
}

使用函数(返回子字符串数组)将时间字符串拆分为三个子字符串如何

现在,数组$substring包含三个子字符串(小时、分钟、秒)。 只需将以下各项相乘即可计算秒数:

$hours = intval($substrings[0]);
$minutes = intval($substrings[1]);
$seconds = intval($substrings[2]);
$seconds = $hours * 3600 + $minutes * 60 + $seconds;

PHP 5.3的替代方案:

<?php

try {
    $date1 = new DateTime('02:00:00');
    $date2 = new DateTime('01:30:00');

    $diff = $date1->diff($date2);

    echo $diff->format('H:i:s');


} catch (Exception $e) {
    echo $e->getMessage();
    exit(1);
}

如果您正在使用PHP日期/时间函数进行现有计算,那么您应该已经在几秒钟内得到了答案。。。也许如果你展示你的代码,我们可以向你指出,但是00:30:00的持续时间不是30秒,而是30分钟或1800分钟seconds@MarkBaker..oh是的,我很抱歉,我将编辑question@Matewka...hahah对不起,输入错误…结果应该是1800…虽然我已经编辑过了…谢谢你的帮助:D
<?php

try {
    $date1 = new DateTime('02:00:00');
    $date2 = new DateTime('01:30:00');

    $diff = $date1->diff($date2);

    echo $diff->format('H:i:s');


} catch (Exception $e) {
    echo $e->getMessage();
    exit(1);
}