Convert seconds into days, hours, minutes and seconds

后端 未结 24 2284
说谎
说谎 2020-11-22 16:30

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

Example:



        
相关标签:
24条回答
  • 2020-11-22 17:07
    foreach ($email as $temp => $value) {
        $dat = strtotime($value['subscription_expiration']); //$value come from mysql database
    //$email is an array from mysqli_query()
        $date = strtotime(date('Y-m-d'));
    
        $_SESSION['expiry'] = (((($dat - $date)/60)/60)/24)." Days Left";
    //you will get the difference from current date in days.
    }
    

    $value come from Database. This code is in Codeigniter. $SESSION is used for storing user subscriptions. it is mandatory. I used it in my case, you can use whatever you want.

    0 讨论(0)
  • 2020-11-22 17:08

    The simplest approach would be to create a method that returns a DateInterval from the DateTime::diff of the relative time in $seconds from the current time $now which you can then chain and format. For example:-

    public function toDateInterval($seconds) {
        return date_create('@' . (($now = time()) + $seconds))->diff(date_create('@' . $now));
    }
    

    Now chain your method call to DateInterval::format

    echo $this->toDateInterval(1640467)->format('%a days %h hours %i minutes'));
    

    Result:

    18 days 23 hours 41 minutes
    
    0 讨论(0)
  • 2020-11-22 17:11

    This is the function rewritten to include days. I also changed the variable names to make the code easier to understand...

    /** 
     * Convert number of seconds into hours, minutes and seconds 
     * and return an array containing those values 
     * 
     * @param integer $inputSeconds Number of seconds to parse 
     * @return array 
     */ 
    
    function secondsToTime($inputSeconds) {
    
        $secondsInAMinute = 60;
        $secondsInAnHour  = 60 * $secondsInAMinute;
        $secondsInADay    = 24 * $secondsInAnHour;
    
        // extract days
        $days = floor($inputSeconds / $secondsInADay);
    
        // extract hours
        $hourSeconds = $inputSeconds % $secondsInADay;
        $hours = floor($hourSeconds / $secondsInAnHour);
    
        // extract minutes
        $minuteSeconds = $hourSeconds % $secondsInAnHour;
        $minutes = floor($minuteSeconds / $secondsInAMinute);
    
        // extract the remaining seconds
        $remainingSeconds = $minuteSeconds % $secondsInAMinute;
        $seconds = ceil($remainingSeconds);
    
        // return the final array
        $obj = array(
            'd' => (int) $days,
            'h' => (int) $hours,
            'm' => (int) $minutes,
            's' => (int) $seconds,
        );
        return $obj;
    }
    

    Source: CodeAid() - http://codeaid.net/php/convert-seconds-to-hours-minutes-and-seconds-(php)

    0 讨论(0)
  • 2020-11-22 17:11

    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

    0 讨论(0)
  • 2020-11-22 17:12
    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.

    0 讨论(0)
  • 2020-11-22 17:13

    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"

    0 讨论(0)
提交回复
热议问题