Problem with time display

Asked

Viewed 113 times

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, thus: 03/02/2017 11:45:55

Does anyone know how I can correct that mistake?

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

  • Where does the variable come from digital in formatarData(digital);. Furthermore, javascript works for 0 until 11 being 0 january and 11 december.

  • Comes from a script in the header: <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>

1 answer

2


How is it possible to check in:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

The month parameter is 0 until 11, being 0 january and 11 feb:

Integer value Representing the Month, Beginning with 0 for January to 11 for December.

To fix your problem, just create, subtract the month value in 1. That is, if the month is 1, must be used 0;

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

Javascript has full ATOM/RFC3339 format support. This format is defined by library constants Datetime. Just use them up

$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 Brasilia (daylight saving time). No javascript, just use this time:

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

To make the passage directly, just:

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

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

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

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

Plus, this is it.

You can see more in the link below https://stackoverflow.com/questions/40916487/php-convert-date-to-javascript-date-january-zero

  • I get it, but I need to keep the code current for other functions to work. It’s an auction script, so it pulls the date and time information from this function, to run the auctions. How could I make this subtraction?

  • It will only change the PHP part, the rest will remain the same. What is the problem?

  • Um, I have no knowledge in this area, which part should I change what is in the header or function?

  • No header ta o var digital = new Date(<?php echo str_replace(':',', ', str_replace('/',', ', str_replace(' ',', ', date('Y/m/d H:i:s')))).', 0'; ?>);

  • @CEW the script in the header, where the variable digital is created. Replace all your line with the code I gave you there in the examples.

  • Gave error: syntax error, Unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in

  • As I commented in the code above, your PHP version is very old, use the second example, with the variables.

  • Ball show, it was not working because I did not change the "date" to "digital", now it worked. Valew partner, great hug and success! ;)

Show 3 more comments

Browser other questions tagged

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