Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!