Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
Using the value of a PHP variable to modify the current time in javascript

I have a PHP page which, among other things, assigns a value to the $deviance variable. The value of this variable is always an integer, generally between 0 and 45.

I then need javascript to use the $deviance value to be added as minutes to current time as determined by javascript.

For example, if $deviance = 10, then a current time of 13:55:10 becomes 14:05:10, so that these values result:

  • var hour = 14
  • var minute = 5
  • var second = 10

In PHP, I calculate the value of $deviance thusly:

$a = new DateTime("$str_deviation");
$b = new DateTime('12:00');

$deviant = $a->diff($b);

$deviance = ( ( $deviant->format('%H') * 60 ) + ( $deviant->format('%i') ) );

Then, I use the PHP variable in javascript:

var deviation=<?php echo json_encode($deviance); ?>;

var now = new Date();
now.setMinutes( now.getMinutes() + deviation);
document.write( now );

var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();

Any help in finding my error will be greatly appreciated!

[Using: Standard LAMP config on Debian 11 server]

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The problem is the use of json_encode.

You can solve the problem by changing

var deviation=<?php echo json_encode($deviance); ?>;

to

var deviation=<?php echo $deviance; ?>;

Thank you to @Lelio and @dave!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda