用于将时间转换为秒数的功能

时间:2010-03-15 23:22:07

标签: php mysql

在我们的网站上,我们有很多游泳时间,我们希望转换为秒。即1:23:33.03或58:22.43。有没有PHP功能可以做到这一点?一个MySQL函数?

7 个答案:

答案 0 :(得分:11)

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_time-to-sec

mysql> SELECT TIME_TO_SEC('22:23:00');
    -> 80580
mysql> SELECT TIME_TO_SEC('00:39:38');
    -> 2378

答案 1 :(得分:8)

function time2seconds($time='00:00:00')
{
    list($hours, $mins, $secs) = explode(':', $time);
    return ($hours * 3600 ) + ($mins * 60 ) + $secs;
}

来自here

MySQL也有TIME_TO_SEC()

答案 2 :(得分:1)

使用以下程序在php中将时间转换为秒。

<?php
function time_to_sec($time) {
    $hours = substr($time, 0, -6);
    $minutes = substr($time, -5, 2);
    $seconds = substr($time, -2);

    return $hours * 3600 + $minutes * 60 + $seconds;
}

$result=time_to_sec('12:25:59');
echo $result;
?>

答案 3 :(得分:1)

$ current_time_in_seconds = “01:21:44.24”;

 list($hours, $minutes, $seconds) = explode(":", $current_time_in_seconds);

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

将来自

01:21:44.24

4904.24

答案 4 :(得分:0)

所以如果mysql没有分数不合适的解决方案 - 这里是另一个我的

$time = '1:23:33.03';

$parts = explode(':', $time);

$seconds = 0;

foreach ($parts as $i => $val) {
    $seconds += $val * pow(60, 2 - $i);
}

echo $seconds;

答案 5 :(得分:-1)

Strtotime就是您所需要的。你得到一个Unix时间戳,在这种情况下是秒数。

答案 6 :(得分:-1)

我有点困惑,但我认为你的意思。 1小时23分钟和23.03秒。

这不难做到。我做了这个PHP函数。我的php服务器不想启动,所以我无法测试它,但我认为它应该可以工作。

function ConvertTimeToSeconds($T)
{
    $exp = explode(":",$T);
    $c = count($exp);
    $r = 0;
    if ($c == 2)
    {
        $r = (((int)$exp[0]) * 60) + ((int)$exp[1]);
    }else{
        $r = (((int)$exp[0]) * 3600) + (((int)$exp[1]) * 60) + ((int)$exp[2]);
    } 

    return $r;
}

ConvertTimeToSeconds("1:23:33.03");