0
I have the following code in the application:
I want to format the time and put another zero when it is only 1 number, I tried to do the following way to have PHP with the strlen function and when it is only 1 digit add another zero in hours/minutes, but I could not.
<?php
date_default_timezone_set('America/Sao_Paulo');
$horaAtualPhp = date('H');// tenho que formatar hora e minutos caso for 1 caracter
// $minutoAtualPhp = date('i');
$minutoAtualPhp = "3";
if(strlen($horaAtualPhp) == 1){
$horaAtualPhp = "0".$horaAtualPhp;
}else{
$horaAtualPhp = $horaAtualPhp;
}
if(strlen($minutoAtualPhp) == 1){
$minutoAtualPhpAtualizado = '0'.$minutoAtualPhp;
}else{
$minutoAtualPhpAtualizado = $minutoAtualPhp;
}
?>
<div class="date">Hora Atual <span id="server_time"></span></div>
<script>
var server_time = document.getElementById("server_time");
var now = new Date(<?=date('Y')?>, <?=date('m')-1?>, <?=date('d')?>, <?=date('H')?>, <?=date('i')?>, <?=date('s')?>, 0);
window.setInterval(function(){
now.setSeconds(now.getSeconds() + 1);
server_time.innerHTML = now.getHours() + ":" + now.getMinutes() ;
},1000);
</script>
var server_time = document.getElementById("server_time");
var now = new Date(<?=date('Y')?>, <?=date('m')-1?>, <?=date('d')?>, <?=date('H')?>, <?=date('i')?>, <?=date('s')?>, 0);
window.setInterval(function(){
now.setSeconds(now.getSeconds() + 1);
server_time.innerHTML = now.getDate() + "/" + (now.getMonth() + 1 )+ "/" + now.getFullYear() + " " + now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
},1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="date">BR, <span id="server_time"></span></div>
<?php
date_default_timezone_set('America/Sao_Paulo');
?>