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
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts

    Live server time error

    I am trying to use PHP/JS to show live server time with the following code:

    PHP Code:
    <script type="text/javascript">

    // Current Server Time script (SSI or PHP)- By JavaScriptKit.com (http://www.javascriptkit.com)
    // For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
    // This notice must stay intact for use.


    var currenttime 'print date("F d, Y H:i:s", time())' //PHP method of getting server date

    ///////////Stop editting here/////////////////////////////////

    var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
    var 
    serverdate=new Date(currenttime)

    function 
    padlength(what){
    var 
    output=(what.toString().length==1)? "0"+what what
    return output
    }

    function 
    displaytime(){
    serverdate.setSeconds(serverdate.getSeconds()+1)
    var 
    datestring=montharray[serverdate.getMonth()]+" "+padlength(serverdate.getDate())+", "+serverdate.getFullYear()
    var 
    timestring=padlength(serverdate.getHours())+":"+padlength(serverdate.getMinutes())+":"+padlength(serverdate.getSeconds())
    document.getElementById("servertime").innerHTML=datestring+" "+timestring
    }

    window.onload=function(){
    setInterval("displaytime()"1000)
    }

    </script> 
    I display it on the page with this:
    PHP Code:
    <b>Current Server Time:</b> <span id="servertime"></span

    This is what it shows on the page:

    undefined NaN, NaN NaN:NaN:NaN
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    What does the source code show? I'm trying to see a way that this code fits into a string that PHP can interpret, but I'm not seeing a way it fits. That would then imply to me that the PHP code is not parsed.
    That said, you must also ensure the format adheres to the following: ECMAScript Language Specification - ECMA-262 Edition 5.1 and this states that the format must be: YYYY-MM-DDTHH:mm:ss.sssZ
    I don't know if there's other format's accepted or not, but that's the only one I could find in there. It appears that the T section can be broken into either / or components as well and will accept partials. But in all cases, it appears that the default format must be the same.

    Personally I'd use the multiple parameter constructor instead.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Do you know of an already built code in PHP or JS that can achieve this? I really only want the Hour and Minutes.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Assuming the JS date object works as I'd expect, than you can simply do:
    PHP Code:
    var currenttime = '<?php print date('h:i');?>'
    You'll need to redo the JS in there though, that appears to be designed to work with full dates.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    Hi

    try

    PHP Code:
    var currenttime =  <?php print '"'date("Y-m-d, H:i:s"time()) .'"'?> ;//PHP method of getting server date

    works for me (see pm)
    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.


  •  

    Posting Permissions

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