How to Convert Seconds into Hours and Minutes in PHP ?

Given a number n (time in seconds), the task is to convert the given number of seconds into hours and minutes in PHP.

Examples:

Input: 3691215
Output: 1025 hours and 20 minutes

Input: 1296
Output: 0 hours and 21 minutes

In PHP, you can convert a duration in seconds into hours and minutes using basic arithmetic operations. To convert the seconds into hours, divide the seconds by 3600, and for minutes, take the remainder of seconds by 3600, and divide by 60.

 

Example: Convert the given time (in seconds) into hours, and minutes using arithmentic operation.

PHP




<?php
  
function secondsToHoursMinutes($seconds) {
      
    // Calculate the hours
    $hours = floor($seconds / 3600);
  
    // Calculate the remaining seconds
    // into minutes
    $minutes = floor(($seconds % 3600) / 60);
  
    // Return the result as an 
    // associative array
    return [
        'hours'   => $hours,
        'minutes' => $minutes,
    ];
}
  
// Driver code
$seconds = 3665;
  
$duration = secondsToHoursMinutes($seconds);
  
echo "{$duration['hours']} hours and"
    . " {$duration['minutes']} minutes";
  
?>


Output

1 hours and 1 minutes

Contact Us