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

Asked

Viewed 34 times

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');
?>

1 answer

4


To convert, just use the command date with the command strtotime. Date prints the formatted output, strtotime converts the string into a timestamp that is leveraged by date.

According to the PHP documentation, H,i and s represent the following values in 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 PHP code execution above.

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

It would be as follows:

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());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.