php 仅将 HH:MM:SS 格式的时间转换为秒?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4834202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:31:09  来源:igfitidea点击:

Convert time in HH:MM:SS format to seconds only?

phptime

提问by Ryan

How to turn time in format HH:MM:SSinto a flat seconds number?

如何将格式中的时间转换HH:MM:SS为平坦的秒数?

P.S.Time could be sometimes in format MM:SSonly.

PS时间有时可能MM:SS只是格式。

回答by Tim Cooper

No need to explodeanything:

不需要explode任何东西:

$str_time = "23:12:95";

$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00::", $str_time);

sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);

$time_seconds = $hours * 3600 + $minutes * 60 + $seconds;

And if you don't want to use regular expressions:

如果您不想使用正则表达式:

$str_time = "2:50";

sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);

$time_seconds = isset($hours) ? $hours * 3600 + $minutes * 60 + $seconds : $minutes * 60 + $seconds;

回答by Glavi?

I think the easiest methodwould be to use strtotime()function:

我认为最简单的方法是使用strtotime()函数:

$time = '21:30:10';
$seconds = strtotime("1970-01-01 $time UTC");
echo $seconds;

// same with objects (for php5.3+)
$time = '21:30:10';
$dt = new DateTime("1970-01-01 $time", new DateTimeZone('UTC'));
$seconds = (int)$dt->getTimestamp();
echo $seconds;

demo

demo



Function date_parse()can also be used for parsing date and time:

函数date_parse()还可用于解析日期和时间:

$time = '21:30:10';
$parsed = date_parse($time);
$seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];

demo

demo



If you will parse format MM:SSwith strtotime()or date_parse()it will fail (date_parse()is used in strtotime()and DateTime), because when you input format like xx:yyparser assumes it is HH:MMand not MM:SS. I would suggest checking format, and prepend 00:if you only have MM:SS.

如果您将解析格式MM:SSstrtotime()否则date_parse()它将失败(date_parse()strtotime()and 中使用DateTime),因为当您输入像xx:yy解析器这样的格式时假定它是HH:MM而不是MM:SS。我建议检查格式,00:如果你只有MM:SS.

demostrtotime()demodate_parse()

demostrtotime()demodate_parse()



If you have hours more than 24, then you can use next function (it will work for MM:SSand HH:MM:SSformat):

如果您的小时数超过 24,那么您可以使用下一个功能(它将适用于MM:SSHH:MM:SS格式化):

function TimeToSec($time) {
    $sec = 0;
    foreach (array_reverse(explode(':', $time)) as $k => $v) $sec += pow(60, $k) * $v;
    return $sec;
}

demo

demo

回答by CanSpice

In pseudocode:

在伪代码中:

split it by colon
seconds = 3600 * HH + 60 * MM + SS

回答by sparsh turkane

    $time = 00:06:00;
    $timeInSeconds = strtotime($time) - strtotime('TODAY');

回答by Chandu

Try this:

尝试这个:

$time = "21:30:10";
$timeArr = array_reverse(explode(":", $time));
$seconds = 0;
foreach ($timeArr as $key => $value)
{
    if ($key > 2) break;
    $seconds += pow(60, $key) * $value;
}
echo $seconds;

回答by mehdi jalilvand

You can use the strtotimefunction to return the number of seconds from today 00:00:00.

您可以使用该strtotime函数从 返回秒数today 00:00:00

$seconds= strtotime($time) - strtotime('00:00:00');

回答by midan888

Simple

简单的

function timeToSeconds($time)
{
     $timeExploded = explode(':', $time);
     if (isset($timeExploded[2])) {
         return $timeExploded[0] * 3600 + $timeExploded[1] * 60 + $timeExploded[2];
     }
     return $timeExploded[0] * 3600 + $timeExploded[1] * 60;
}

回答by simme

<?php
$time    = '21:32:32';
$seconds = 0;
$parts   = explode(':', $time);

if (count($parts) > 2) {
    $seconds += $parts[0] * 3600;
}
$seconds += $parts[1] * 60;
$seconds += $parts[2];