Porto

Convert Seconds To Hours Minutes Seconds Words

Convert Seconds To Hours Minutes Seconds Words

Here is a PHP function that will convert seconds into hours, minutes and seconds. The function makes use of bcmod to do the modulo operations and with some simple strings, the result is returned. The demonstration uses the PHP time() function to get the number of seconds since EPOCH, but any interger will be converted to the hours minutes seconds interpretation.


<?php

/**
 *
 * @convert seconds to hours minutes and seconds
 *
 * @param int $seconds The number of seconds
 *
 * @return string
 *
 */
function secondsToWords($seconds)
{
    
/*** return value ***/
    
$ret "";

    
/*** get the hours ***/
    
$hours intval(intval($seconds) / 3600);
    if(
$hours 0)
    {
        
$ret .= "$hours hours ";
    }
    
/*** get the minutes ***/
    
$minutes bcmod((intval($seconds) / 60),60);
    if(
$hours || $minutes 0)
    {
        
$ret .= "$minutes minutes ";
    }
  
    
/*** get the seconds ***/
    
$seconds bcmod(intval($seconds),60);
    
$ret .= "$seconds seconds";

    return 
$ret;
}
?>

Example Usage


<?php
    
/*** time since EPOCH ***/
    
echo secondsToWords(time());
?>

Demonstration

Time since EPOCH

390604 hours 11 minutes 51 seconds