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?

3
  • Just echo your server time directly as a value for your JavaScript variable. Commented Apr 14, 2015 at 13:33
  • possible duplicate of javascript clock with server time Commented Apr 14, 2015 at 13:39
  • I got the server time correctly. My problem is, How Can i increment it? For example, i got, 21:33:01, I want to make it 21:33:02 after a minute. Commented Apr 14, 2015 at 13:43

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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);

3 Comments

It still not changing.
What does the console log?
I got the server time correctly. My problem is, How Can i increment it? For example, i got, 21:33:01, I want to make it 21:33:02 after a minute
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> 

1 Comment

If you believe the questions are asking the same think flag it as a duplicate.
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.