Skip to content

Instantly share code, notes, and snippets.

@sineld
Last active February 19, 2018 14:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sineld/f392f4a7efd470b8401c to your computer and use it in GitHub Desktop.
Save sineld/f392f4a7efd470b8401c to your computer and use it in GitHub Desktop.
Create Your Own jQuery Digital Clock
/*
Create Your Own jQuery Digital Clock
http://www.sitepoint.com/create-jquery-digital-clock-jquery4u/
*/
<script>
function updateClock()
{
var currentTime = new Date ( );
var currentHours = currentTime.getHours ( );
var currentMinutes = currentTime.getMinutes ( );
var currentSeconds = currentTime.getSeconds ( );
currentHours = ( currentHours < 10 ? "0" : "" ) + currentHours;
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds;
$(".timer").html(currentTimeString);
}
$(document).ready(function()
{
setInterval('updateClock()', 1000);
});
</script>
<span class="timer"></span>
<?php
$date = new DateTime(date('H:i:s'), new DateTimeZone('Asia/Dubai'));
$current_timestamp = $date->getTimestamp();
?>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
var now = new Date(<?=$current_timestamp*1000?>);
function updateTime(){
var nowMS = now.getTime();
nowMS += 1000;
now.setTime(nowMS);
var currentHours = now.getHours();
var currentMinutes = now.getMinutes();
var currentSeconds = now.getSeconds();
currentHours = (currentHours < 10 ? "0" : "" ) + currentHours;
currentMinutes = (currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentSeconds = (currentSeconds < 10 ? "0" : "" ) + currentSeconds;
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds;
$(".timer").html(currentTimeString);
}
$(document).ready(function()
{
setInterval('updateTime()', 1000);
});
</script>
<span class="timer"></span><br /><br /><br />
<!--
https://chelladuraii.wordpress.com/2012/06/18/live-timer-using-jquery-ajax-and-php/
-->
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
setInterval(ajaxcall, 1000);
});
function ajaxcall(){
$.ajax({
url: 'time.php',
success: function(data) {
data = data.split(':');
$('#hours').html(data[0]);
$('#minutes').html(data[1]);
$('#seconds').html(data[2]);
}
});
}
</script>
<span id="hours">0</span>:<span id="minutes">0</span>:<span id="seconds">0</span>
<?php
echo $date = date('h:i:s A');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment