-3

I have php date:

<?php
date_default_timezone_set('Europe/London');
 echo date('D M d Y H:i:s O');
?>

and javascript/jquery script to publish the date on website:

setInterval(function() {
var d = new Date("<?  echo date('D M d Y H:i:s O'); ?>")
currentHours = d.getHours();
currentHours = ("0" + currentHours).slice(-2);
currentMinutes = d.getMinutes();
currentMinutes = ("0" + currentMinutes).slice(-2);
currentSec = d.getSeconds();
currentSec = ("0" + currentSec).slice(-2);



    $('#timer').text((currentHours +':' +  currentMinutes + ':' + currentSec ));
},

But javascript return aN:aN:aN How to insert server time into javascript?

magic-s
  • 242
  • 1
  • 8

3 Answers3

1

You can simply use new Date() as

<script>
    function myFunction(obj) {
        var today = new Date("<?php echo date('Y-m-d H:i:s')?>");
        var hh = today.getHours();
        var ii = today.getMinutes();
        var ss = today.getSeconds();
        if (hh < 10) {
            hh = '0' + hh
        }
        if (ii < 10) {
            ii = '0' + ii
        }
        if (ss < 10) {
            ss = '0' + ss
        }


        obj.value = hh + ':' + ii + ':' + ss;
    }
</script>

<input type="text" onclick="myFunction(this);">
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • Yes. but javascript picks device timer but not server. And server time is syncronized but device is not. That is the reason why i need show the server time – magic-s Jun 15 '15 at 14:39
0

Use the code as follow :

I just replaced new Date("<? echo date('D M d Y H:i:s O'); ?>") to

new Date("<?php echo date('D M d Y H:i:s O'); ?>")

its working for me

var d = new Date("<?php echo date('D M d Y H:i:s O'); ?>")
        alert(d);
currentHours = d.getHours();
currentHours = ("0" + currentHours).slice(-2);
currentMinutes = d.getMinutes();
currentMinutes = ("0" + currentMinutes).slice(-2);
currentSec = d.getSeconds();
currentSec = ("0" + currentSec).slice(-2);



    $('#timer').text((currentHours +':' +  currentMinutes + ':' + currentSec ));
Rohitashv Singhal
  • 4,517
  • 13
  • 57
  • 105
  • When `short_open_tags` is enabled on your server (is on most commercial servers), you could use `new Date("= echo date('D M d Y H:i:s O'); ?>")` as well (note the added `=` right after ``). – BillyNate Jun 15 '15 at 12:02
  • but the same problem with the question asker may be short_open_tags is not enabled with him – Rohitashv Singhal Jun 15 '15 at 12:03
  • @lord_linus fine but problem the time is static. but i need to show live time. If new Date() then timer shows current time every 1 s but with php i need to update manualy – magic-s Jun 15 '15 at 12:04
  • 1
    @lord_lines - Might be, that's why I mentioned it needs to be enabled. I'm not saying you answer is wrong, just saying it can be done in different ways ;) – BillyNate Jun 15 '15 at 12:05
  • @magic-s - That's not what you mentioned in the question. Please be more clear about what you want to accomplish. – BillyNate Jun 15 '15 at 12:06
  • 1
    then replace o (Oh) with 0(zero) – Rohitashv Singhal Jun 15 '15 at 12:06
0

You made a typo in the php date, it should be a 0 (zero) instead of a O at the end of the string.

setInterval(function() {
var d = new Date("<?  echo date('D M d Y H:i:s 0'); ?>")
currentHours = d.getHours();
currentHours = ("0" + currentHours).slice(-2);
currentMinutes = d.getMinutes();
currentMinutes = ("0" + currentMinutes).slice(-2);
currentSec = d.getSeconds();
currentSec = ("0" + currentSec).slice(-2);



    $('#timer').text((currentHours +':' +  currentMinutes + ':' + currentSec ));
},

See also https://jsfiddle.net/r3b56h2o/ for a working answer.

unicorn80
  • 1,107
  • 2
  • 9
  • 15