如何将秒转换为时间格式?

发布于 2024-09-26 04:17:43 字数 354 浏览 0 评论 0原文

由于某种原因,我将时间格式转换为:03:30 为秒 3*3600 + 30*60,现在。我想将其转换回其第一个(相同)格式。怎么可能呢?

我的尝试:

3*3600 + 30*60 = 12600 

12600 / 60 = 210 / 60 = 3.5, floor(3.5) = 3 = hour

现在,会议记录怎么样?

考虑该值可能类似于 19:00 或 02:51。 我想你已经明白了。

顺便问一下,如何使用 RegEx 将 2:0 转换为 02:00?

For some reason I convert a time format like: 03:30 to seconds 3*3600 + 30*60, now. I wanna convert it back to its first (same) format up there. How could that be?

My attempt:

3*3600 + 30*60 = 12600 

12600 / 60 = 210 / 60 = 3.5, floor(3.5) = 3 = hour

Now, what about the minutes?

Considering the value can be like 19:00 or 02:51.
I think you got the picture.

And by the way, how to convert 2:0 for example to 02:00 using RegEx?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

执妄 2024-10-03 04:17:43

这可能更简单

gmdate("H:i:s", $seconds)

PHP gmdate

This might be simpler

gmdate("H:i:s", $seconds)

PHP gmdate

绅刃 2024-10-03 04:17:43
$hours = floor($seconds / 3600);
$mins = floor($seconds / 60 % 60);
$secs = floor($seconds % 60);

如果你想获取时间格式:

$timeFormat = sprintf('%02d:%02d:%02d', $hours, $mins, $secs);
$hours = floor($seconds / 3600);
$mins = floor($seconds / 60 % 60);
$secs = floor($seconds % 60);

If you want to get time format:

$timeFormat = sprintf('%02d:%02d:%02d', $hours, $mins, $secs);
枫以 2024-10-03 04:17:43

如果您知道时间将少于一小时,则可以使用 date()$date->format() 函数。

$minsandsecs = date('i:s',$numberofsecs);

这是有效的,因为系统纪元时间从午夜开始(1970 年 1 月 1 日,但这对您来说并不重要)。

如果是一个小时或更长但少于一天,您可以以小时:分钟:秒的格式输出,并带有 `

$hoursminsandsecs = date('H:i:s',$numberofsecs);

对于超过一天,您需要使用模数来计算天数,因为这是该纪元的开始日期将变得相关。

希望有帮助。

If the you know the times will be less than an hour, you could just use the date() or $date->format() functions.

$minsandsecs = date('i:s',$numberofsecs);

This works because the system epoch time begins at midnight (on 1 Jan 1970, but that's not important for you).

If it's an hour or more but less than a day, you could output it in hours:mins:secs format with `

$hoursminsandsecs = date('H:i:s',$numberofsecs);

For more than a day, you'll need to use modulus to calculate the number of days, as this is where the start date of the epoch would become relevant.

Hope that helps.

阿楠 2024-10-03 04:17:43

也许最简单的方法是:

gmdate('H:i:s', $your_time_in_seconds);

Maybe the simplest way is:

gmdate('H:i:s', $your_time_in_seconds);
风流物 2024-10-03 04:17:43

$time 为以秒为单位的时间。

$seconds = $time % 60;
$time = ($time - $seconds) / 60;
$minutes = $time % 60;
$hours = ($time - $minutes) / 60;

现在,小时、分钟和秒分别为 $hours$mins$seconds

Let $time be the time as number of seconds.

$seconds = $time % 60;
$time = ($time - $seconds) / 60;
$minutes = $time % 60;
$hours = ($time - $minutes) / 60;

Now the hours, minutes and seconds are in $hours, $minutes and $seconds respectively.

〆凄凉。 2024-10-03 04:17:43

另一种解决方案将为您提供传入秒值的天、小时、分钟和秒:

function seconds_to_time($secs)
{
    $dt = new DateTime('@' . $secs, new DateTimeZone('UTC'));
    return array('days'    => $dt->format('z'),
                 'hours'   => $dt->format('G'),
                 'minutes' => $dt->format('i'),
                 'seconds' => $dt->format('s'));
}

print_r(seconds_to_time($seconds_value);

如果预计时间超过一年,则“天”将需要额外的逻辑。使用 str_pad()ltrim() 添加/删除前导零。

Another solution that will give you the days, hours, minutes, and seconds for a passed-in seconds value:

function seconds_to_time($secs)
{
    $dt = new DateTime('@' . $secs, new DateTimeZone('UTC'));
    return array('days'    => $dt->format('z'),
                 'hours'   => $dt->format('G'),
                 'minutes' => $dt->format('i'),
                 'seconds' => $dt->format('s'));
}

print_r(seconds_to_time($seconds_value);

Extra logic will be needed for 'days' if the time is expected to be more than one year. Use str_pad() or ltrim() to add/remove leading zeros.

梦醒灬来后我 2024-10-03 04:17:43

当您想使用此代码将秒数转换为时间格式(如 小时:分钟:秒)时,ITroubs 答案不会处理剩余的秒数,

这是我处理此问题的方法:
(这还会向一位数分钟和秒添加前导零)

$seconds = 3921; //example

$hours = floor($seconds / 3600);
$mins = floor(($seconds - $hours*3600) / 60);
$s = $seconds - ($hours*3600 + $mins*60);

$mins = ($mins<10?"0".$mins:"".$mins);
$s = ($s<10?"0".$s:"".$s); 

$time = ($hours>0?$hours.":":"").$mins.":".$s;

在此示例中 $time 将包含“1:05:21”。

ITroubs answer doesn't deal with the left over seconds when you want to use this code to convert an amount of seconds to a time format like hours : minutes : seconds

Here is what I did to deal with this:
(This also adds a leading zero to one-digit minutes and seconds)

$seconds = 3921; //example

$hours = floor($seconds / 3600);
$mins = floor(($seconds - $hours*3600) / 60);
$s = $seconds - ($hours*3600 + $mins*60);

$mins = ($mins<10?"0".$mins:"".$mins);
$s = ($s<10?"0".$s:"".$s); 

$time = ($hours>0?$hours.":":"").$mins.":".$s;

$time will contain "1:05:21" in this example.

当梦初醒 2024-10-03 04:17:43

如果您要对其进行硬编码,您将按照其他人的建议使用模数来提取时间。

如果您要从 MySQL 数据库返回秒数,假设您的应用程序中不需要秒格式的数据,则有一种更简洁的方法可以做到这一点,您可以使用 MySQL 的 SEC_TO_TIME 它将返回 hh:mm:ss 格式的时间格式。

例如。

SELECT SEC_TO_TIME(my_seconds_field) AS my_timestring;

If you were to hardcode it you would use modulus to extract the time as others suggested.

If you are returning the seconds from MySQL database, assuming you don't need the data in seconds format in your app, there is a much cleaner way to do it, you can use MySQL's SEC_TO_TIME and it will return time in hh:mm:ss format.

Eg.

SELECT SEC_TO_TIME(my_seconds_field) AS my_timestring;
梦忆晨望 2024-10-03 04:17:43

抱歉,这已经太晚了,但也许有用

function mediaTimeDeFormater($seconds)
{
    if (!is_numeric($seconds))
        throw new Exception("Invalid Parameter Type!");


    $ret = "";

    $hours = (string )floor($seconds / 3600);
    $secs = (string )$seconds % 60;
    $mins = (string )floor(($seconds - ($hours * 3600)) / 60);

    if (strlen($hours) == 1)
        $hours = "0" . $hours;
    if (strlen($secs) == 1)
        $secs = "0" . $secs;
    if (strlen($mins) == 1)
        $mins = "0" . $mins;

    if ($hours == 0)
        $ret = "$mins:$secs";
    else
        $ret = "$hours:$mins:$secs";

    return $ret;
}

echo mediaTimeDeFormater(216.064000);//3:36

Sorry this is too late but maybe useful

function mediaTimeDeFormater($seconds)
{
    if (!is_numeric($seconds))
        throw new Exception("Invalid Parameter Type!");


    $ret = "";

    $hours = (string )floor($seconds / 3600);
    $secs = (string )$seconds % 60;
    $mins = (string )floor(($seconds - ($hours * 3600)) / 60);

    if (strlen($hours) == 1)
        $hours = "0" . $hours;
    if (strlen($secs) == 1)
        $secs = "0" . $secs;
    if (strlen($mins) == 1)
        $mins = "0" . $mins;

    if ($hours == 0)
        $ret = "$mins:$secs";
    else
        $ret = "$hours:$mins:$secs";

    return $ret;
}

echo mediaTimeDeFormater(216.064000);//3:36
三生一梦 2024-10-03 04:17:43

像这样的东西?

if(is_numeric($time)){
    $value = array(
        "years" => 0, "days" => 0, "hours" => 0,
        "minutes" => 0, "seconds" => 0,
    );
    if($time >= 31556926){
        $value["years"] = floor($time/31556926);
        $time = ($time%31556926);
    }
    if($time >= 86400){
        $value["days"] = floor($time/86400);
        $time = ($time%86400);
    }
    if($time >= 3600){
        $value["hours"] = floor($time/3600);
        $time = ($time%3600);
    }
    if($time >= 60){
        $value["minutes"] = floor($time/60);
        $time = ($time%60);
    }

    $value["seconds"] = floor($time);
    return (array) $value;
    
} else{
    return (bool) FALSE;
}

摘自:http://www.ckorp.net/sec2time.php

something like this?

if(is_numeric($time)){
    $value = array(
        "years" => 0, "days" => 0, "hours" => 0,
        "minutes" => 0, "seconds" => 0,
    );
    if($time >= 31556926){
        $value["years"] = floor($time/31556926);
        $time = ($time%31556926);
    }
    if($time >= 86400){
        $value["days"] = floor($time/86400);
        $time = ($time%86400);
    }
    if($time >= 3600){
        $value["hours"] = floor($time/3600);
        $time = ($time%3600);
    }
    if($time >= 60){
        $value["minutes"] = floor($time/60);
        $time = ($time%60);
    }

    $value["seconds"] = floor($time);
    return (array) $value;
    
} else{
    return (bool) FALSE;
}

grabbed from: http://www.ckorp.net/sec2time.php

白况 2024-10-03 04:17:43

使用模:

$hours = $time_in_seconds / 3600;
$minutes = ($time_in_seconds / 60) % 60;

Use modulo:

$hours = $time_in_seconds / 3600;
$minutes = ($time_in_seconds / 60) % 60;
尛丟丟 2024-10-03 04:17:43

仅一个小的附加示例

请求以毫秒为单位的时间

    // ms2time( (microtime(true) - ( time() - rand(0,1000000) ) ) );
    // return array
    function ms2time($ms){
        $return = array();
        // ms
        $return['ms'] = (int) number_format( ($ms - (int) $ms), 2, '', '');
        $seconds = (int) $ms;
        unset($ms);

        if ($seconds%60 > 0){
            $return['s'] = $seconds%60;
        } else {
            $return['s'] = 0;
        }

        if ( ($minutes = intval($seconds/60))){
            $return['m'] = $minutes;
        }

        if (isset($return['m'])){
            $return['h'] = intval($return['m'] / 60);
            $return['m']  = $return['m'] % 60; 
        }


        if (isset($return['h'])){
            $return['d'] = intval($return['h'] / 24);
            $return['h']  = $return['h'] % 24; 
        }

        if (isset($return['d']))
            $return['mo'] = intval($return['d'] / 30);

        foreach($return as $k=>$v){
            if ($v == 0)
                unset($return[$k]);
        }

        return $return;
    }

    // ms2time2string( (microtime(true) - ( time() - rand(0,1000000) ) ) );
    // return array     
    function ms2time2string($ms){
        $array = array(
            'ms' => 'ms',
            's'  => 'seconds',
            'm'  => 'minutes',
            'h'  => 'hours',
            'd'  => 'days',
            'mo' => 'month',
        );


        if ( ( $return = ms2time($ms) )  && count($ms) > 0){

            foreach($return as $key=>$data){
                $return[$key] = $data .' '.$array[$key];
            }

        }
        return implode(" ", array_reverse($return));
    }

just one small additional example

requested time in miliseconds

    // ms2time( (microtime(true) - ( time() - rand(0,1000000) ) ) );
    // return array
    function ms2time($ms){
        $return = array();
        // ms
        $return['ms'] = (int) number_format( ($ms - (int) $ms), 2, '', '');
        $seconds = (int) $ms;
        unset($ms);

        if ($seconds%60 > 0){
            $return['s'] = $seconds%60;
        } else {
            $return['s'] = 0;
        }

        if ( ($minutes = intval($seconds/60))){
            $return['m'] = $minutes;
        }

        if (isset($return['m'])){
            $return['h'] = intval($return['m'] / 60);
            $return['m']  = $return['m'] % 60; 
        }


        if (isset($return['h'])){
            $return['d'] = intval($return['h'] / 24);
            $return['h']  = $return['h'] % 24; 
        }

        if (isset($return['d']))
            $return['mo'] = intval($return['d'] / 30);

        foreach($return as $k=>$v){
            if ($v == 0)
                unset($return[$k]);
        }

        return $return;
    }

    // ms2time2string( (microtime(true) - ( time() - rand(0,1000000) ) ) );
    // return array     
    function ms2time2string($ms){
        $array = array(
            'ms' => 'ms',
            's'  => 'seconds',
            'm'  => 'minutes',
            'h'  => 'hours',
            'd'  => 'days',
            'mo' => 'month',
        );


        if ( ( $return = ms2time($ms) )  && count($ms) > 0){

            foreach($return as $key=>$data){
                $return[$key] = $data .' '.$array[$key];
            }

        }
        return implode(" ", array_reverse($return));
    }
月下伊人醉 2024-10-03 04:17:43

这是另一种方法,所有这些都以“0”开头。

$secCount = 10000;
$hours = str_pad(floor($secCount / (60*60)), 2, '0', STR_PAD_LEFT);
$minutes = str_pad(floor(($secCount - $hours*60*60)/60), 2, '0', STR_PAD_LEFT);
$seconds = str_pad(floor($secCount - ($hours*60*60 + $minutes*60)), 2, '0', STR_PAD_LEFT);

这是根据 Flaxious 的答案改编的。

Here is another way with leading '0' for all of them.

$secCount = 10000;
$hours = str_pad(floor($secCount / (60*60)), 2, '0', STR_PAD_LEFT);
$minutes = str_pad(floor(($secCount - $hours*60*60)/60), 2, '0', STR_PAD_LEFT);
$seconds = str_pad(floor($secCount - ($hours*60*60 + $minutes*60)), 2, '0', STR_PAD_LEFT);

It is an adaptation from the answer of Flaxious.

拥抱我好吗 2024-10-03 04:17:43

如果您想要好的格式,例如:0:00:00,请使用 str_pad() 作为@Gardner。

If You want nice format like: 0:00:00 use str_pad() as @Gardner.

夏尔 2024-10-03 04:17:43

1 天 = 86400000 毫秒。

解码时间(毫秒/86400000,小时,分钟,秒,毫秒)

Ups!我在想,在delphi中,所有语言中一定有类似的东西。

1 day = 86400000 milliseconds.

DecodeTime(milliseconds/86400000,hr,min,sec,msec)

Ups! I was thinking in delphi, there must be something similar in all languages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文