0

I want to get the server time using php, and pass it to a javascript variable. Once it is passed, it will increment.

Here is my code:

function initTime(date){
var today=new Date(date);
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('time').innerHTML = h+":"+m+":"+s;
}
function checkTime(i) {
if (i<10) {i = "0" + i};  // add zero in front of numbers < 10
return i;
}

Situation: The user browse welcome page, the server time is sent and received by javascript.

I want to increment it, to make it a clock script. I don't want to get the server time every second using php, because it will use more server resources.

How can I increment it using javascript?

Pinas De Radio
  • 131
  • 2
  • 7

4 Answers4

1

You can put your php variable in an hidden input :

<input id="your_input" value="<?php echo $my_var; ?>">

and get it in javascript with :

my_var = document.getElementById('your_input').value;

Dovre
  • 71
  • 1
  • 10
1

Two ways. First, using moment.js:

setInterval(function() {
    document.getElementById('time').innerHTML = moment().format('HH:mm:ss');
},1000);

or using your function:

function initTime(date){
var today=new Date(date);
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('time').innerHTML = h+":"+m+":"+s;
}
function checkTime(i) {
if (i<10) {i = "0" + i};  // add zero in front of numbers < 10
return i;
}

setInterval(initTime(date),1000);
baao
  • 71,625
  • 17
  • 143
  • 203
1

I found another question that was basically after the same thing. The initialized the JS date using the milliseconds from php:

<script type="text/javascript">
    var d = new Date(<?php echo time() * 1000 ?>);
</script> 

Here is a jQuery clock plugin that you can use that you can feed a starting time to(your server time). Putting it all together, it would go something like this:

<script type="text/javascript">
    var d = new Date(<?php echo time() * 1000 ?>);
    $("#clock").clock({"timestamp":d.getTime()});
</script> 
Community
  • 1
  • 1
Paul Zepernick
  • 1,452
  • 1
  • 11
  • 25
1

You can just output the PHP date somewhere, either in a global variable, or as a data attribute somewhere you can get it

<div id="time" data-now="<?php echo time() * 1000; ?>"></div>

and then

var elem  = document.getElementById('time');
var timer = setInterval(initTime, 1000);

function initTime() {
    var time = +(elem.getAttribute('data-now')),
    time = time + 1000; // add one second

    var today = new Date( time ),
        h = today.getHours(),
        m = today.getMinutes(),
        s = today.getSeconds();

        m = checkTime(m);
        s = checkTime(s);

    elem.innerHTML = h + ":" + m + ":" + s;
    elem.setAttribute('data-now', today.getTime());
}

function checkTime(i) {
    if (i < 10) {
        i = "0" + i
    }; // add zero in front of numbers < 10
    return i;
}

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388