Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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-将小时转换为分钟_Php_Min_Hour - Fatal编程技术网

PHP-将小时转换为分钟

PHP-将小时转换为分钟,php,min,hour,Php,Min,Hour,我经常使用这个网站,最后我发现了一些我似乎能弄明白的东西。我有一个时间返回: "1hr 59min" 我希望它将该值转换为分钟(119) 我尝试过各种strotime方法,但似乎没有一种能奏效。有什么想法吗 干杯 Stu阅读php文档并使用正确的函数,例如,等等。如果使用REGEX,则使用preg_replace() 如果您的输入只能有hr或min,则在上面添加以下两行: $str = preg_replace("/(\d+)hr/e", "$1*60", $str); $str = preg

我经常使用这个网站,最后我发现了一些我似乎能弄明白的东西。我有一个时间返回:

"1hr 59min"
我希望它将该值转换为分钟(119)

我尝试过各种strotime方法,但似乎没有一种能奏效。有什么想法吗

干杯


Stu

阅读php文档并使用正确的函数,例如,等等。

如果使用REGEX,则使用preg_replace()

如果您的输入只能有hr或min,则在上面添加以下两行:

$str = preg_replace("/(\d+)hr/e", "$1*60", $str);
$str = preg_replace("/(\d+)min/", "$1", $str);

我想你可以用正则表达式:

function convertToSeconds($time)
{
    $matches = array();
    if (preg_match('/^(\d+)hr (\d+)min$/', $time, $matches) {
        return $matches[1]*60 + $matches[2];
    }
}

如果它是一根弦,你可以做

$onefiftynine = "1hr 59min";
$split        = explode(' ',$onefiftynine);
$hour         = substr($split[0],0,strpos($split[0],'hr'));
$min          = substr($split[1],0,strpos($split[1],'min'));

echo $hour * 60 + $min;

你不能只用小时*60+分钟吗?你的原始数据是什么?我建议尝试
DateInterval::createFromDateString(“1hr 59min”)
,但解析器似乎无法识别“hr”。如果这是您的输入数据,您必须根据下面的答案自己解析它。
$onefiftynine = "1hr 59min";
$split        = explode(' ',$onefiftynine);
$hour         = substr($split[0],0,strpos($split[0],'hr'));
$min          = substr($split[1],0,strpos($split[1],'min'));

echo $hour * 60 + $min;