javascript clock with PHP date help needed

Discussion in 'JavaScript' started by phantom, Mar 8, 2011.

  1. #1
    I am try to get the current server time into javascript clock where the clock starts ticking up to show the current running time but can't seem to get it to work.

    The code I am using so far is:

    function show2(){
      if (!document.all)
        return
    
      var Digital=new Date("<? echo $mytime; ?>")
      var hours=Digital.getHours()
      var minutes=Digital.getMinutes()
      var seconds=Digital.getSeconds()
      var dn="AM" 
    
      if (hours>12){
       dn="PM"
       hours=hours-12
      }
    
      if (hours==0)
       hours=12
    
      if (minutes<=9)
       minutes="0"+minutes
    
      if (seconds<=9)
       seconds="0"+seconds
       var ctime=hours+":"+minutes+":"+seconds+" "+dn
       tick2.innerHTML="<b style='font-size:22;color:blue;'>"+ctime+"</b>"
       setTimeout("show2()",1000)
    }
    
    window.onload=show2
    
    Code (markup):

    Anyone have any idea as to what I am doing wrong or have a better solution to get it to work?

    Thanks in advance.
     
    phantom, Mar 8, 2011 IP
  2. rashgang

    rashgang Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    what is in the mytime variable $mytime can you post it here
     
    rashgang, Mar 9, 2011 IP
  3. rashgang

    rashgang Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <form name="Tick">
    <input type="text" size="11" name="Clock">
    </form>
    <script>


    function show(){
    var Digital=new Date(<?php $mytime; ?>)
    var hours=Digital.getHours()
    var minutes=Digital.getMinutes()
    var seconds=Digital.getSeconds()
    var dn="AM"
    if (hours>12){
    dn="PM"
    hours=hours-12
    }
    if (hours==0)
    hours=12
    if (minutes<=9)
    minutes="0"+minutes
    if (seconds<=9)
    seconds="0"+seconds
    document.Tick.Clock.value=hours+":"+minutes+":"
    +seconds+" "+dn
    setTimeout("show()",1000)
    }
    show()

    </script>

    Please try with this script

    The above script you are using is not defined span "tick2"
     
    rashgang, Mar 9, 2011 IP