...

View Full Version : Dynamic Javascript Clock



Troy297
08-27-2007, 05:34 PM
Okay...

So I've made myself a working clock that gets the time accuratly in PHP but I need help on making it so that I can integrate it into my dynamic javascript clock....



<?php
$oh = "-4"; //Offset for hours
$om = "-20"; //Offset for minutes
list($hour, $minute, $second) = explode(' ', gmdate('H i s'));
$newdate = gmdate('H:i:s A', gmmktime($hour + $oh, $minute + $om, $second));
?>

Now I just can't figure out how to get the same effect/result using Javascript... here's the clock:


<script language="JavaScript">
<?php
$mins = "-20";
$hrs = "-4";
?>
function jsClockTimeZone(){
// Copyright 1999 - 2001 by Ray Stott
// Script available at http://www.crays.com/jsc
var time = new Date()
var hour = time.getUTCHours()+<?php echo $hrs;?>
var minute = time.getUTCMinutes()+<?php echo $mins;?>
var second = time.getUTCSeconds()
var temp = "" + ((hour > 12) ? hour - 12 : hour)
if(hour==0) temp = "12"
temp += ((minute < 10) ? ":0" : ":") + minute
temp += ((second < 10) ? ":0" : ":") + second
temp += (hour >= 12) ? " PM" : " AM"
document.clockFormTimeZone.digits.value = temp
setTimeout("jsClockTimeZone()",1000)
}
//-->
</script>

You see the problem is that the Javascript doesn't do the adding in "24-hour mode" as I would want it to do... Is there anyway for the Javascript to get its vars for hours and minutes from the PHP and then have them re-evaluate themselves every 50 seconds or something?

Thanks again!

mcjwb
08-27-2007, 10:38 PM
You could always do the adding in 24 hour mode yourself;)


<?php
$mins = "-20";
$hrs = "-4";
?>
function jsClockTimeZone(){
// Copyright 1999 - 2001 by Ray Stott
// Script available at http://www.crays.com/jsc
var time = new Date();
var hour = time.getUTCHours()+<?php echo $hrs;?>;
if(hour>23){
hour = hour-23;
}
else if(hour<0){
23+hour;
}
var minute = time.getUTCMinutes()+<?php echo $mins;?>
if(minute>59){
minute = minute-60;
hour++;
}
else if(minute<0){
minute = 60+minute;
hour--;
}
var second = time.getUTCSeconds()
var temp = "" + ((hour > 12) ? hour - 12 : hour)
if(hour==0) temp = "12"
temp += ((minute < 10) ? ":0" : ":") + minute
temp += ((second < 10) ? ":0" : ":") + second
temp += (hour >= 12) ? " PM" : " AM";
document.clockFormTimeZone.digits.value = temp
setTimeout("jsClockTimeZone()",1000)
}

Troy297
08-28-2007, 02:11 AM
Okay thanks for that, but a couple problems....

a) If I could do it myself then I wouldn't be posting it now would it? ;)
b) The code you provided doesn't work.

So I guess I'll just end up using PHP with a refresh... alot easier! Unless anyone else has any suggestions?

Thanks.

Philip M
08-28-2007, 07:50 AM
If it doesn't work, you could try making it work.

else if(hour <0 ){
hour = 23+hour;
}


var temp = "" + ((hour > 24) ? hour - 24 : hour)
if (hour == 0) temp = "00"
temp += ((minute < 10) ? ":0" : ":") + minute
temp += ((second < 10) ? ":0" : ":") + second
// temp += (hour >= 12) ? " PM" : " AM"; // delete

Troy297
08-30-2007, 01:57 AM
Okay.. I've got it working now how I wanted :) I kinda learned from the examples and worked it out myself! Thanks!


function jsClockTimeZone(){
// Copyright 1999 - 2001 by Ray Stott
// Script available at http://www.crays.com/jsc
var time = new Date();
var hour = time.getUTCHours()+<?php echo $hrs;?>;
if(hour>23){
hour = hour-24;
}
else if(hour<0){
hour = 24+hour;
}
var minute = time.getUTCMinutes();
if(minute>59){
minute = minute-60;
hour++;
}
else if(minute<0){
minute = 60+minute;
hour--;
}
var second = time.getSeconds();
var temp = "" + ((hour > 12) ? hour - 12 : hour)
if (hour == 0) temp = "00"
temp += ((minute < 10) ? ":0" : ":") + minute
temp += ((second < 10) ? ":0" : ":") + second
temp += (hour >= 12) ? " PM" : " AM";
document.clockFormTimeZone.digits.value = temp
setTimeout("jsClockTimeZone()",1000)
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum