PHP:时间格式

时间:2013-04-06 16:41:39

标签: php time

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

2 个答案:

答案 0 :(得分:4)

这听起来更像是sscanf的工作,然后是任何时间功能。

这样的事情:

<?php

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

$minutes += 60 * $hours;

echo "total minutes: $minutes\n";

答案 1 :(得分: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);