Consider the following.
Example: https://jsfiddle.net/Twisty/yft97sme/
JavaScript
$(function() {
$.widget("app.serverTime", {
options: {
server: "https://worldtimeapi.org/api/ip",
showLabel: true,
labelValue: "Server Time:"
},
_create: function() {
var self = this;
self.element.addClass("ui-server-time ui-widget");
if (self.options.showLabel) {
$("<label>", {
class: "ui-server-time-label",
style: "margin-right: 5px;"
}).html(self.options.labelValue).appendTo(self.element);
}
$("<span>", {
class: "ui-server-time-display"
}).appendTo(self.element);
$("<button>", {
class: "ui-server-time-refresh",
style: "margin-left: 5px;"
}).html("Refresh").button({
icon: "ui-icon-arrowreturnthick-1-s",
showLabel: false
}).click(function() {
self.getTime();
}).hide().appendTo(self.element);
self.getTime();
},
_log: function(str) {
console.log(Date.now(), str);
},
_serverTime: null,
_collectTime: function() {
var self = this;
self.beforeGetTime();
$.ajax({
cache: false,
type: "get",
url: this.options.server,
success: function(results) {
self._log("Collected Time: " + results.unixtime);
self._setTime(results);
}
});
},
_setTime: function(obj) {
this._log("Time Setter");
this._serverTime = obj
},
_getTime: function() {
this._log("Time Getter");
var dt = new Date(this._serverTime.datetime);
$(".ui-server-time-display", this.element).html(dt.toString());
$(".ui-server-time-refresh", this.element).show();
return this._serverTime;
},
beforeGetTime: function() {
this._log("Before Get Time");
},
getTime: function() {
this._collectTime();
this._delay(this._getTime, 500);
},
_destroy: function() {
this.element.children().remove();
this.element.removeclass("ui-server-time");
}
});
$(".clock").serverTime({
showLabel: false
});
});
If you run the GET in the creation of the widget, it will show the Server Time from that moment. It will also delay the creation if the server is slow to respond. I would suggest moving the collection of the time to it's own function. This way you can call it whenever you want.
Assuming your PHP Script might return a different value, you may need to adjust the result handling.