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 = 14var minute = 5var second = 10In 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]
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!