Convert seconds to Hour:Minute:Second

2018-12-31 10:00发布

I need to convert seconds to "Hour:Minute:Second".

For example: "685" converted to "00:11:25"

How can I achieve this?

标签: php time
23条回答
孤独寂梦人
2楼-- · 2018-12-31 10:30
gmdate("H:i:s", no_of_seconds);

Will not give time in H:i:s format if no_of_seconds is greater than 1 day (seconds in a day).
It will neglect day value and give only Hour:Min:Seconds

For example:

gmdate("H:i:s", 89922); // returns 0:58:42 not (1 Day 0:58:42) or 24:58:42
查看更多
谁念西风独自凉
3楼-- · 2018-12-31 10:30

A simple way to use DateTime for this is:

    $time = 60; //sec.
    $now = time();
    $rep = new DateTime('@'.$now);
    $diff = new DateTime('@'.($now+$time));
    $return = $diff->diff($rep)->format($format);

    //output:  01:04:65

It's a simple solution wich gives you the ability to use the format Method of DateTime.

查看更多
萌妹纸的霸气范
4楼-- · 2018-12-31 10:31

Here is a one liner that handles negative seconds and more than 1 day worth of seconds.

sprintf("%s:%'02s:%'02s\n", intval($seconds/60/60), abs(intval(($seconds%3600) / 60)), abs($seconds%60));

For Example:

$seconds= -24*60*60 - 2*60*60 - 3*60 - 4; // minus 1 day 2 hours 3 minutes 4 seconds
echo sprintf("%s:%'02s:%'02s\n", intval($seconds/60/60), abs(intval(($seconds%3600) / 60)), abs($seconds%60));

outputs: -26:03:04

查看更多
春风洒进眼中
5楼-- · 2018-12-31 10:32
function timeToSecond($time){
    $time_parts=explode(":",$time);
    $seconds= ($time_parts[0]*86400) + ($time_parts[1]*3600) + ($time_parts[2]*60) + $time_parts[3] ; 
    return $seconds;
}

function secondToTime($time){
    $seconds  = $time % 60;
    $seconds<10 ? "0".$seconds : $seconds;
    if($seconds<10) {
        $seconds="0".$seconds;
    }
    $time     = ($time - $seconds) / 60;
    $minutes  = $time % 60;
    if($minutes<10) {
        $minutes="0".$minutes;
    }
    $time     = ($time - $minutes) / 60;
    $hours    = $time % 24;
    if($hours<10) {
        $hours="0".$hours;
    }
    $days     = ($time - $hours) / 24;
    if($days<10) {
        $days="0".$days;
    }

    $time_arr = array($days,$hours,$minutes,$seconds);
    return implode(":",$time_arr);
}
查看更多
ら面具成の殇う
6楼-- · 2018-12-31 10:35

One hour is 3600sec, one minute is 60sec so why not:

<?php

$init = 685;
$hours = floor($init / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;

echo "$hours:$minutes:$seconds";

?>

which produces:

$ php file.php
0:11:25

(I've not tested this much, so there might be errors with floor or so)

查看更多
十年一品温如言
7楼-- · 2018-12-31 10:37

Just in case anyone else is looking for a simple function to return this nicely formatted (I know it is not the format the OP asked for), this is what I've just come up with. Thanks to @mughal for the code this was based on.

function format_timer_result($time_in_seconds){
    $time_in_seconds = ceil($time_in_seconds);

    // Check for 0
    if ($time_in_seconds == 0){
        return 'Less than a second';
    }

    // Days
    $days = floor($time_in_seconds / (60 * 60 * 24));
    $time_in_seconds -= $days * (60 * 60 * 24);

    // Hours
    $hours = floor($time_in_seconds / (60 * 60));
    $time_in_seconds -= $hours * (60 * 60);

    // Minutes
    $minutes = floor($time_in_seconds / 60);
    $time_in_seconds -= $minutes * 60;

    // Seconds
    $seconds = floor($time_in_seconds);

    // Format for return
    $return = '';
    if ($days > 0){
        $return .= $days . ' day' . ($days == 1 ? '' : 's'). ' ';
    }
    if ($hours > 0){
        $return .= $hours . ' hour' . ($hours == 1 ? '' : 's') . ' ';
    }
    if ($minutes > 0){
        $return .= $minutes . ' minute' . ($minutes == 1 ? '' : 's') . ' ';
    }
    if ($seconds > 0){
        $return .= $seconds . ' second' . ($seconds == 1 ? '' : 's') . ' ';
    }
    $return = trim($return);

    return $return;
}
查看更多
登录 后发表回答