How can PHP convert seconds into a format of days:hours:minutes:seconds?
To convert seconds into a format of days:hours:minutes:seconds in PHP, you can use simple arithmetic operations to calculate the number of days, hours, minutes, and seconds from the total number of seconds. You can then format these values into a string in the desired format.
function convertSecondsToDHMS($seconds) {
$days = floor($seconds / (60 * 60 * 24));
$hours = floor(($seconds % (60 * 60 * 24)) / (60 * 60));
$minutes = floor(($seconds % (60 * 60)) / 60);
$seconds = $seconds % 60;
return "$days:$hours:$minutes:$seconds";
}
$seconds = 100000;
echo convertSecondsToDHMS($seconds); // Output: 1:3:46:40
Keywords
Related Questions
- What best practices should be followed when implementing error handling in PHP code for form submissions?
- What are the best practices for handling variables and queries in PHP to avoid SQL injection vulnerabilities?
- What are the benefits of using numerical values instead of text in HTML select options when working with PHP code?