Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something built into PHP to convert seconds to days, hours, mins?

Tags:

php

For example if I have:

$seconds = 3744000; // i want to output: 43 days, 8 hours, 0 minutes

Do I have to create a function to convert this? Or does PHP already have something built in to do this like date()?

like image 710
supercoolville Avatar asked Nov 27 '22 17:11

supercoolville


1 Answers

function secondsToWords($seconds)
{
    $ret = "";

    /*** get the days ***/
    $days = intval(intval($seconds) / (3600*24));
    if($days> 0)
    {
        $ret .= "$days days ";
    }

    /*** get the hours ***/
    $hours = (intval($seconds) / 3600) % 24;
    if($hours > 0)
    {
        $ret .= "$hours hours ";
    }

    /*** get the minutes ***/
    $minutes = (intval($seconds) / 60) % 60;
    if($minutes > 0)
    {
        $ret .= "$minutes minutes ";
    }

    /*** get the seconds ***/
    $seconds = intval($seconds) % 60;
    if ($seconds > 0) {
        $ret .= "$seconds seconds";
    }

    return $ret;
}

print secondsToWords(3744000);
like image 187
Ian Gregory Avatar answered Dec 06 '22 04:12

Ian Gregory