How can one efficiently convert a total number of seconds into a formatted time string with days, hours, minutes, and seconds in PHP?

When converting a total number of seconds into a formatted time string with days, hours, minutes, and seconds in PHP, you can achieve this by using simple arithmetic operations to calculate the individual components of time. This can be done by dividing the total seconds by the respective units (86400 seconds in a day, 3600 seconds in an hour, 60 seconds in a minute) and then formatting the result into a string.

function formatTime($totalSeconds) {
    $days = floor($totalSeconds / 86400);
    $hours = floor(($totalSeconds % 86400) / 3600);
    $minutes = floor(($totalSeconds % 3600) / 60);
    $seconds = $totalSeconds % 60;

    return sprintf('%d days, %d hours, %d minutes, %d seconds', $days, $hours, $minutes, $seconds);
}

$totalSeconds = 123456;
echo formatTime($totalSeconds); // Output: 1 days, 10 hours, 17 minutes, 36 seconds