Convert seconds into days, hours, minutes and seco

2019-01-01 02:26发布

I would like to convert a variable $uptime which is seconds, into days, hours, minutes and seconds.

Example:

$uptime = 1640467;

Result should be:

18 days 23 hours 41 minutes

标签: php date
18条回答
忆尘夕之涩
2楼-- · 2019-01-01 02:31

Here it is a simple 8-lines PHP function that converts a number of seconds into a human readable string including number of months for large amounts of seconds:

PHP function seconds2human()

查看更多
流年柔荑漫光年
3楼-- · 2019-01-01 02:36

This can be achieved with DateTime class

Use:

echo secondsToTime(1640467);
# 18 days, 23 hours, 41 minutes and 7 seconds

Function:

function secondsToTime($seconds) {
    $dtF = new \DateTime('@0');
    $dtT = new \DateTime("@$seconds");
    return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}

demo

查看更多
千与千寻千般痛.
4楼-- · 2019-01-01 02:38

an extended version of Glavić's excellent solution , having integer validation, solving the 1 s problem, and additional support for years and months, at the expense of being less computer parsing friendly in favor of being more human friendly:

<?php
function secondsToHumanReadable(/*int*/ $seconds)/*: string*/ {
    //if you dont need php5 support, just remove the is_int check and make the input argument type int.
    if(!\is_int($seconds)){
        throw new \InvalidArgumentException('Argument 1 passed to secondsToHumanReadable() must be of the type int, '.\gettype($seconds).' given');
    }
    $dtF = new \DateTime ( '@0' );
    $dtT = new \DateTime ( "@$seconds" );
    $ret = '';
    if ($seconds === 0) {
        // special case
        return '0 seconds';
    }
    $diff = $dtF->diff ( $dtT );
    foreach ( array (
            'y' => 'year',
            'm' => 'month',
            'd' => 'day',
            'h' => 'hour',
            'i' => 'minute',
            's' => 'second' 
    ) as $time => $timename ) {
        if ($diff->$time !== 0) {
            $ret .= $diff->$time . ' ' . $timename;
            if ($diff->$time !== 1 && $diff->$time !== -1 ) {
                $ret .= 's';
            }
            $ret .= ' ';
        }
    }
    return substr ( $ret, 0, - 1 );
}

var_dump(secondsToHumanReadable(1*60*60*2+1)); -> string(16) "2 hours 1 second"

查看更多
何处买醉
5楼-- · 2019-01-01 02:42
gmdate("d H:i:s",1640467);

Result will be 19 23:41:07. When it is just one second more than normal day, it is increasing the day value for 1 day. This is why it show 19. You can explode the result for your needs and fix this.

查看更多
永恒的永恒
6楼-- · 2019-01-01 02:42

Solution that should exclude 0 values and set correct singular/plural values

use DateInterval;
use DateTime;

class TimeIntervalFormatter
{

    public static function fromSeconds($seconds)
    {
        $seconds = (int)$seconds;
        $dateTime = new DateTime();
        $dateTime->sub(new DateInterval("PT{$seconds}S"));
        $interval = (new DateTime())->diff($dateTime);
        $pieces = explode(' ', $interval->format('%y %m %d %h %i %s'));
        $intervals = ['year', 'month', 'day', 'hour', 'minute', 'second'];
        $result = [];
        foreach ($pieces as $i => $value) {
            if (!$value) {
                continue;
            }
            $periodName = $intervals[$i];
            if ($value > 1) {
                $periodName .= 's';
            }
            $result[] = "{$value} {$periodName}";
        }
        return implode(', ', $result);
    }
}
查看更多
旧时光的记忆
7楼-- · 2019-01-01 02:43

Interval class I have written can be used. It can be used in opposite way too.

composer require lubos/cakephp-interval

$Interval = new \Interval\Interval\Interval();

// output 2w 6h
echo $Interval->toHuman((2 * 5 * 8 + 6) * 3600);

// output 36000
echo $Interval->toSeconds('1d 2h');

More info here https://github.com/LubosRemplik/CakePHP-Interval

查看更多
登录 后发表回答