Skip to content
PHP Logo

How can the conversion between decimal numbers and hours/minutes be optimized in PHP calculations?

When converting between decimal numbers and hours/minutes in PHP calculations, one way to optimize the process is by using built-in functions like floor() and round() to extract the hours and minutes from the decimal number. This allows for accurate conversion without the need for complex mathematical operations.

php
// Function to convert decimal number to hours and minutes
function convertDecimalToTime($decimal) {
    $hours = floor($decimal);
    $minutes = round(($decimal - $hours) * 60);
    
    return "$hours hours $minutes minutes";
}

// Example usage
$decimalNumber = 3.75;
$time = convertDecimalToTime($decimalNumber);
echo $time; // Output: 3 hours 45 minutes