1

我知道,这应该很容易,但我在 PHP 中的时间函数有问题。我有一个像“1 h 38 min”这样的字符串,我想将它转换为带有分钟数的整数。任何帮助还是微不足道的?;)

4

2 回答 2

4

这听起来更像是sscanf的工作,而不是任何时间函数。

像这样的东西:

<?php

$input = '1 h 38 min';
sscanf($input, '%d h %d min', $hours, $minutes);

$minutes += 60 * $hours;

echo "total minutes: $minutes\n";
于 2013-04-06T16:49:41.313 回答
0

下面是一些比 sscanf 更健壮的代码,并且更具体地使用时间(并且适用于旧版本的 PHP):

$input = '1 d 1 hour 38 min';
$usableInput = trim(str_replace(array(' y ', ' m ', ' d ', ' h ', ' s '), array(' years ', ' months ', ' days ', ' hours ', ' seconds '), " $input "));
$now = time();
$seconds = strtotime("+$usableInput", $now) - $now;
$minutes = floor($seconds / 60);
于 2013-04-06T19:47:11.163 回答