Lachie
Lachie

Reputation: 1411

Creating a Timer using php and ajax

Could someone assist me in creating a live database timer.

It will have a start time in database, I am using PHP and am hoping to use js or ajax whatever works best for what I am trying to complete.

I've written some pseudo-like code to illustrate how I plan to do this:

BEGIN

Request server time
// adjust server time with client time

get serverTime
get clientTime

adjustment = serverTime - clientTime
timeData = timeData - adjustment

THEN 
send timeData to server

END

Upvotes: 0

Views: 2164

Answers (1)

Domain
Domain

Reputation: 11808

<div id="time">
    <span id="hour">hh</span>:<span id="min">mm</span>:<span id="sec">ss</span>
</div>

setInterval(update, 1000);
function update() {
  var date = new Date()

  var hours = date.getHours()
  if (hours < 10) hours = '0'+hours
  document.getElementById('hour').innerHTML = hours

  var minutes = date.getMinutes()
  if (minutes < 10) minutes = '0'+minutes
  document.getElementById('min').innerHTML = minutes

  var seconds = date.getSeconds()
  if (seconds < 10) seconds = '0'+seconds
  document.getElementById('sec').innerHTML = seconds
}

You don't need to use ajax or php you can do it with simple javascript

Upvotes: 2

Related Questions