|
-
October 20th, 2008, 07:42 AM
#4
Re: Server Time Count Down Clock
 Originally Posted by TheCPUWizard
JavaScript is probably the easiest way to go.
JavaScript cannot access the server-side. And, since the OP wants the time from the server, there is going to have to be some implementation from the server.
I wrote a script a while back that first grabs the time from the server, and then puts the values into JavaScript variables. However, the client-side will not be 100% accurate. Accuracy will vary machine to machine. But, since the majority of webpages are only viewed for a minute or two, the time will be relatively close to the server.
PHP Code:
<?php
$hour = 0;
$minute = 0;
$month = 12;
$day = 25;
$year = 2008;
$event = 'Christmas';
$remaining = date('U', mktime($hour, $minute, 0, $month, $day, $year)) - date('U');
$days = floor($remaining / 60 / 60 / 24);
$hours = $remaining / 60 / 60 % 24;
$minutes = $remaining / 60 % 60;
$seconds = $remaining % 60;
?>
<div id="countdown"></div>
<script type="text/javascript">
var days = '<?php echo $days; ?>';
var hours = '<?php echo $hours; ?>';
var minutes = '<?php echo $minutes; ?>';
var seconds = '<?php echo $seconds; ?>';
var finished = false;
function updatecountdown(){
seconds--;
if(seconds < 0){
seconds = 59;
minutes--;
if(minutes < 0){
minutes = 59;
hours--;
if(hours < 0){
hours = 23;
days--;
if(days < 0){
finished = true;
}
}
}
}
if(!finished){
var message = days + ' days<br />';
message += hours + ' hours<br />';
message += minutes + ' minutes<br />';
message += seconds + ' seconds<br />';
message += 'until <?php echo $event; ?>!';
document.getElementById('countdown').innerHTML = message;
}
else{
document.getElementById('countdown').innerHTML = '<?php echo $event; ?>!';
clearInterval(theInterval);
}
}
var theInterval = setInterval("updatecountdown()", 1000);
</script>
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|