Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Question Dynamic Javascript Clock

    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 Code:
    <?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:

    PHP Code:
    <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!
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    You could always do the adding in 24 hour mode yourself

    Code:
    <?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) 
    }

  • #3
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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.
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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

  • #5
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Okay.. I've got it working now how I wanted I kinda learned from the examples and worked it out myself! Thanks!

    Code:
    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) 
    }
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •