What is the best way to convert a number of seconds into a format like TT:HH:MM:SS in PHP?
To convert a number of seconds into a format like TT:HH:MM:SS in PHP, you can use the following logic: calculate the total hours, minutes, and seconds from the given number of seconds, and then format them into the desired format. You can achieve this by using the modulus operator (%) to extract the hours, minutes, and seconds from the total seconds.
function formatSeconds($seconds) {
$hours = floor($seconds / 3600);
$minutes = floor(($seconds % 3600) / 60);
$seconds = $seconds % 60;
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
// Example usage
$seconds = 3661;
echo formatSeconds($seconds); // Output: 01:01:01