How to add another zero in the hours for example: 4: 4: 03 becomes 4:04:03?

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 count as PHP with the function strlen and when it is only 1 digit I 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><divclass="date">BR, <span id="server_time"></span></div>

<?php
date_default_timezone_set('America/Sao_Paulo');
?>
    
asked by anonymous 05.07.2017 / 13:21

1 answer

4

To convert, simply use the command date with the command strtotime . The date prints the output formatted, strtotime converts the string to a timestamp that is used by date.

  

According to the PHP documentation, H, i and s represent the following values on date:

H   Formato 24-horas de uma hora com zero à esquerda    00 até 23
i   Minutos com zero à esquerda 00 até 59
s   Segundos, com zero à esquerda   00 até 59

It would look like this:

<?php

$hora = "4:4:03";
echo date("H:i:s", strtotime($hora)); //04:04:03

?>

Link of running PHP code above.

Now in javaScript, you can extend the string object by adding a function to format the time.

It would look like this:

String.prototype.formatarHora = function() {
  var _temporario = this.split(':');

  _temporario = _temporario.map(function(valor) {
    _saida = parseInt(valor);
    _saida = (_saida < 10 ? "0" + valor : valor);
    return _saida;
  });

  return _temporario.join(':');
}

let hora = "4:4:13";
console.log("Hora sem estar formatada: %s", hora);
console.log("Hora formatada: %s", hora.formatarHora());
    
05.07.2017 / 13:31