Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From what I gather, it looks like you're interested in returning a string with values containing two digits separaed by colons. Assuming the positioning doesn't need to change, you can do something similar to the following:</p> <pre><code>&lt;?php // Prefix single-digit values with a zero. function ensure2Digit($number) { if($number &lt; 10) { $number = '0' . $number; } return $number; } // Convert seconds into months, days, hours, minutes, and seconds. function secondsToTime($ss) { $s = ensure2Digit($ss%60); $m = ensure2Digit(floor(($ss%3600)/60)); $h = ensure2Digit(floor(($ss%86400)/3600)); $d = ensure2Digit(floor(($ss%2592000)/86400)); $M = ensure2Digit(floor($ss/2592000)); return "$M:$d:$h:$m:$s"; } </code></pre> <p>Or if you don't like the thought of having one more function to manage, perhaps this may suit you better:</p> <pre><code>&lt;?php // Convert seconds into months, days, hours, minutes, and seconds. function secondsToTime($ss) { $s = $ss%60; $m = floor(($ss%3600)/60); $h = floor(($ss%86400)/3600); $d = floor(($ss%2592000)/86400); $M = floor($ss/2592000); // Ensure all values are 2 digits, prepending zero if necessary. $s = $s &lt; 10 ? '0' . $s : $s; $m = $m &lt; 10 ? '0' . $m : $m; $h = $h &lt; 10 ? '0' . $h : $h; $d = $d &lt; 10 ? '0' . $d : $d; $M = $M &lt; 10 ? '0' . $M : $M; return "$M:$d:$h:$m:$s"; } </code></pre> <p>And then to call our function (whichever method you decide to use):</p> <pre><code>&lt;?php $ss = 123456; print secondsToTime($ss); ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload