#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Mt. Laurel, NJ
    Posts
    4
    Rep Power
    0

    Unhappy javascript time/date in hidden field


    I need to to have a hidden field in a form that contains the current time & date to be sent to a VARCHAR(255) field in a mysql database.

    The rest of the form is handled with PHP, but I can only get he server time with it.

    Can anyone help me figure out how to do this, my javascript is arusty, to say the least.

    thanks,

    Steve
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    North Dakota
    Posts
    53
    Rep Power
    14
    Try this:


    <html>
    <head>
    <script LANGUAGE="JavaScript">

    function dates (){
    var d = new Date(); //creates a new date
    var dd = d.getDate(); //retrieves the date from the system
    var mm = d.getMonth() + 1; //retrieves the month JS starts counting at zero, so add a 1 to start January at 1
    var yyyy = d.getYear(); //retrieves the year
    var now = mm + "/" + dd + "/" + yyyy; //formats the date*/

    document.formName.sysDt.value = now; //puts the value retrieved from the system in the sysDt field
    }

    </script>
    </head>

    <body onLoad="dates()">
    <form name="formName">
    <input type="text" name="sysDt">

    </form>
    </body>
    </html>

    If you want the field to be hidden just change the input type to "hidden"
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    New Zealand
    Posts
    167
    Rep Power
    14

    do you want 'real' time, or user's time?


    Hi Steve,

    alshawver has it right if you want to get the user's time... however you might find this has as many problems as getting the server time, for example the user may not just be out by a few minutes, but may not even have the correct day and year on their system - very buggy for database data.

    I guess you can't adjust the server time? If not, I guess you could try something along these lines, especially if you're in the States... Even if not, I use the Universal time for my own date into database functions.

    The output of the script is something like: July 23, 2001, 23:35:05

    <?php
    // put the timer.pl output into an array
    $fcontents = file ('http://tycho.usno.navy.mil/cgi-bin/timer.pl');
    while (list ($line_num, $line) = each ($fcontents))
    {
    if ($line_num == 3)
    //Line 3 is universal time, line 5 Eastern Daylight, etc. Take a look at the source of the timer.pl page for the rest of them.
    {
    $line = str_replace ("<B>", "", $line);
    $line = str_replace ("Universal Time", "", $line);
    $line = trim ($line);
    echo $line;
    }
    }
    ?>

    Does anyone know of a site that offers 'real-time' date data to coders? Sure would be usefull....

    Jen
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Mt. Laurel, NJ
    Posts
    4
    Rep Power
    0

    Wink server time problem


    the problem with using server time is that for some reason no matter what time it is it's always X:07, like the server only refreshes it's time every hour at 7 minutes after.

    i dont host this site myself, so i dont have any access to the server's controls.

    thanks for the javascript help though, its funny. i've been doing so much php and coldfusion, i could barely remember how to write javascript!

    thanks all!

IMN logo majestic logo threadwatch logo seochat tools logo