3v4l.org

run code in 300+ PHP versions simultaneously
<?php function formatHMS($inputval, $full = false, $timeFormat = '') { $minus = ''; if ($inputval < 0) { $minus = '- '; $inputval = -$inputval; } $unith = 3600; // Num of seconds in an Hour... $unitm = 60; // Num of seconds in a min... $hh = (int)($inputval / $unith); // '/' given value by num sec in hour... output = HOURS $ss_remaining = ($inputval - ($hh * 3600)); // '*' number of hours by seconds, then '-' from given value... output = REMAINING seconds $mm = (int)($ss_remaining / $unitm); // take remaining sec and devide by sec in a min... output = MINS $ss = ($ss_remaining - ($mm * 60)); // '*' number of mins by seconds, then '-' from remaining sec... output = REMAINING seconds. if (!$full && $ss > 30) { if ($mm === 60) { $hh++; $mm = 0; } } if ($timeFormat == '' || $timeFormat == 0) { if ($full) { return $minus . round($hh) . 'h ' . sprintf("%02d", round($mm)) . 'm ' . sprintf("%02d", round($ss)) . 's'; } if ($hh > 0) { return $minus . round($hh) . 'h ' . sprintf("%02d", round($mm)).'m'; } if ($mm > 0) { return $minus . sprintf("%02d", round($mm)).'m'; } return $minus . round($ss) . 's'; } switch ($timeFormat) { case 1: if ($full) { return $minus . sprintf("%02d", round($hh)) . ':' . sprintf("%02d", round($mm)) . ':' . sprintf("%02d", round($ss)); } return $minus . sprintf("%02d", round($hh)) . ':' . sprintf("%02d", round($mm)); case 2: return $minus . str_replace(".", ",", number_format(round($hh) + ($mm + ($ss / $unitm)) / $unitm, 2)); case 3: return $minus . number_format(round($hh) + ($mm + ($ss / $unitm)) / $unitm, 2); } } function test(){ echo formatHMS(10021, false, 0) . PHP_EOL; echo formatHMS(10021, false, 1) . PHP_EOL; echo formatHMS(10021, false, 2) . PHP_EOL; echo formatHMS(10021, false, 3) . PHP_EOL; echo formatHMS(10021, true, 0) . PHP_EOL; echo formatHMS(10021, true, 1) . PHP_EOL; echo formatHMS(10021, true, 2) . PHP_EOL; echo formatHMS(10021, true, 3) . PHP_EOL; } for($i = 0; $i < 1000; $i++){ test(); }

preferences:
57.64 ms | 402 KiB | 5 Q