Lets break down the question
<?php echo time() ?>
This is generated server-side and cannot be re-calculated in the client side (where your code actually runs).
var time = <?php echo time() ?>;
Here you are re-declaring (and by this masking) the original time
variable.
console.log(time);
This is being called outside of the interval function scope, so it will only run once (and in that time in will print null
).
You are looking for something like this :
setInterval(function(){
console.log(Date())
}, 1000);
If you want your variable to be accessiable outside the interval's function's scope you can do something like this
var time;
setInterval(function(){
time = Date();
console.log(time);
}, 1000);
// Now the 'time' variable will be accessible and will hold the latest date value
// For example console.log(time)
And of course you can replace Date()
with any date/time creating function you will need for you specific purposes.