Problem with time display

1

Dae galera, with a little problem here, I have the following function:

function atualizaHoraServidor() {
  var dispTime = formatarData(digital);

  $('#horarioServidor .horarioRelogio').text(dispTime);

  digital.setSeconds(digital.getSeconds() + 1);

  setTimeout("atualizaHoraServidor()", 1000);
}

function formatarData(data) {
  var options = {
    year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: '2-digit'
};
  return data.toLocaleDateString('pt-br', options);  
}

The variable digital is created with the following script:

<script type="text/javascript"> var digital = new Date(<?php echo str_replace(':',', ', str_replace('/',', ', str_replace(' ',', ', date('Y/m/d H:i:s')))).', 0'; ?>); </script>

What is happening is that it is showing the date with 1 month in advance, this way: 03/02/2017 11:45:55

Does anyone know how I can correct this error?

I've checked the server time and it's correct.

    
asked by anonymous 03.01.2017 / 14:48

1 answer

2

How can you check in:

link

The month parameter is 0 to 11 , being 0 january and 11 february:

  

Integer value representing the month, beginning with 0 for January to 11 for December.

To correct your problem, simply create the subtract month value by 1. That is, if the month is 1 , 0 should be used;

However, your code is doing a lot more than necessary and creating a complete mess. There is a much simpler medium.

javascript has full support for the ATOM / RFC3339 format. This format is defined by the DateTime library constants. Just use them

$date = new DateTime();
echo $date->format(DateTime::ATOM);

The above example will print:

  

2017-01-03T12: 39: 58-02: 00

What is the current time in Brasília (with daylight saving time). In javascript, just use this time:

var date = new Date("2017-01-03T12:39:58-02:00");

To make the pass directly, just:

var date = new Date("<?= (new DateTime())->format(DateTime::ATOM) ?>"); // PHP >= 5.4

And so it will be correct for your code to use it as such.

If your PHP does not support the above expression (PHP 5.3), you will have to create variables:

<?php $date = new DateTime(); ?>
var date = new Date("<?php echo $date->format(DateTime::ATOM) ?>");

No more, that's it.

You can see more in the link below link

    
03.01.2017 / 15:01