UTC clock based on server time?

I have a JavaScript clock that gets the time in UTC, it currently works, but the time is based off the client's computer. What could I do to base the time off of the server instead? I am using PHP as the server scripting language. I would like to not use AJAX.

$year   = date("Y");
$month  = date("m");
$day    = date("d");
$hour   = date("h");
$minute = date("i");
$str    = $year . $month . $day . $hour . $minute;
echo "history.pushState('', 'title', '?q=$str');";
echo "var ct = '$str';";
function dT(){
    var d = new Date();
    d = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds());
    v = d.getFullYear() + "" +
            padstr(d.getMonth()) + "" +
            padstr(d.getDate()) + "" +
            padstr(d.getHours()) + "" +
            padstr(d.getMinutes()) + "" + padstr(d.getSeconds());
    if(ct !== v){
        history.pushState('', 'title', '?q=' + v);
        ct = v;
    setTimeout('dT()', 1000);


    var a = moment().format("<?php echo date("Y-m-d H:i:s", time()); ?>");
    document.getElementById("time").innerHTML = a;
    function clock_tick(){
        var time = moment(a);
        time.add('second', 1);
        a = time.format("YYYY-MM-DD HH:MM:SS");
        document.getElementById("time").innerHTML = a;
        setTimeout("clock_tick()", 1000);


This should get you started. You don't need to use moment.js, but I wanted to try it out since someone suggested it.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="moment.js"></script>
<script language="javascript">
$(function() {
    $('#clock')[0].innerHTML = moment().format("<?php echo date("Y-m-d H:i:s", time()); ?>");
function clock_tick(){
    var clock_div = $('#clock')[0];
    var time = moment(clock_div.innerHTML);
    time.add('second', 1);
    clock_div.innerHTML = time.format("YYYY-MM-DD hh:mm:ss");
    setTimeout("clock_tick()", 1000);
<div id="clock"></div>

Then as I stated earlier you may need to set:


Reference Link: http://php.net/manual/en/function.date.php

Need Your Help

Can apache-jmeter check the return value of a URL for a correct response?

testing automated-tests jmeter

The set up for apache-jmeter allows for a URL to be sent to a web-server on multiple threads. I'm interested in first determining if the response codes are 200-500 and then whether the returned co...

In bash, how to tail a fifo that is currently being written to, and then tail the fifo again, grepping for different text?

bash unix tail fifo

I have a process that is writing to standard out, and I want to be able to monitor the output by grepping for various strings while running tail -f. One way to do this is to write to a normal file ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.