Convertire i secondi in formato time con PHP & JS
Posted on 08. Aug, 2009 by daniele in guide per il web, javascript, php, script ajax, script php, utility
CODICE PHP
function formatTime($secs) { $times = array(3600, 60, 1); $time = ''; $tmp = ''; for($i = 0; $i < 3; $i++) { $tmp = floor($secs / $times[$i]); if($tmp < 1) { $tmp = '00'; } elseif($tmp < 10) { $tmp = '0' . $tmp; } $time .= $tmp; if($i < 2) { $time .= ':'; } $secs = $secs % $times[$i]; } return $time; }
CODICE JAVASCRIPT
function formatTime(secs){ var times = new Array(3600, 60, 1); var time = ''; var tmp; for(var i = 0; i < times.length; i++){ tmp = Math.floor(secs / times[i]); if(tmp < 1){ tmp = '00'; } else if(tmp < 10){ tmp = '0' + tmp; } time += tmp; if(i < 2){ time += ':'; } secs = secs % times[i]; } return time; }
Come potete vedere le funzioni sono identiche, dovete solo scegliere (in base alle vostre esigenze) quale delle due applicare



Leave a reply