I'm trying to make this work, but are having some problems?!?

I use a javascript to get the users local date & time and that part works perfect, but when I try to insert it into the table it comes out like this: 0000-00-00 00:00:00?

How do I get this in to my db table in a date/time field?

<script type="text/javascript"> 
function GetTime() { 
var now = new Date(); 

var test = now.getFullYear() + '-' + 
(now.getMonth() < 9 ? '0' : '') + (now.getMonth()+1) + '-' + 
(now.getDate() < 10 ? '0' : '') + now.getDate() + ' ' + 
(now.getHours() < 10 ? '0' : '') + now.getHours() + ':' + 
(now.getMinutes() < 10 ? '0' : '') + now.getMinutes() + ':' + 
(now.getSeconds() < 10 ? '0' : '') + now.getSeconds() 
; 

document.write(test); 
} 

</script> 

<? 
$localDate = "<script language=javascript>GetTime();</script>"; 

echo $localDate; 

mysql_query("INSERT INTO ".$prefix."_users (fname,lname,email,date) VALUES ('$fname','$lname','$email','$localDate')"); 
?>

Thanks in advance :-)

    first: switch to mysqli at some point.

    second: show us an example of what the echo $localDate prints out

    third: I would suggest you skip all the processing in the javascript and simply pass the date in whatever format you get and then simply process it in PHP like so (assuming that you passed the date into the variable $myDate):

    $localDate = date("Y-m-d H:i:s", strtotime($myDate));
    

    fourth: dont use short tags

      Bjom;10930655 wrote:

      first: switch to mysqli at some point.

      Not sure what you mean and why?

      Bjom;10930655 wrote:

      second: show us an example of what the echo $localDate prints out

      2009-10-13 12:19:24

      Bjom;10930655 wrote:

      third: I would suggest you skip all the processing in the javascript and simply pass the date in whatever format you get and then simply process it in PHP like so (assuming that you passed the date into the variable $myDate):

      I need the date and time of the users local machine... I don't see how I get that by your example?

      Bjom;10930655 wrote:

      fourth: dont use short tags

      Not sure what you mean?

        1. mysqli

        2. 2009-10-13 12:19:24
          looks good. if it doesn't contain any hidden chars it 'should' work.
          2a) try to run that through the one-liner I provided an see if it improves the situation.

        3. How do you get the date from the users local machine in your example? Do it exactly in the same way but forego the processing. (I.e.: immediately pass the value of the java script's "new Date()". My example does not cover that. It only shows you what to do once you got the date from the users machine.

        4. Quick google or search on the manual would point you here: full tags vs. short tags. I read up in the manual very often it is a very good resource.

          If I skip the javascript my date comes out like this: Tue Oct 13 12:43:36 UTC+0200 2009.

          So I need to format the date in the javascript... The format of the localDate is perfect, but I cant pass it into my dbtable... Thats all I want?!

          I ran it to the oneliner with no luck...

          Hmmm... What to do?

            no you do not have to format the date in the javacript. simply try the code that I provided. It does the formatting and does so efficiently.

            Check your db Table for the exact type of the field storing the dates.

              ...and if things don't work as expected...

              track down the error starting at the bottom. In this case: Get any tool that allows you to run queries against your DB, preferrably MySQL Administrator. Copy your query in there and test it, providing the values manually. Next copy and paste the value for the date and see what happens. If that works continue by feeding the variable $localDate manually in the PHP code, etc....working your way slowly up to the top (i.e the javascript).

                I dont see any code here to get the data from the browser to the server ????

                You need to run the JavaScript in the browser, then put the result into a FORM field and get the user to submit the data, or use a auto submit command in JavaScript.

                This example gets the data from javascript and submits it to the server. You can get the data using $_GET["localtime"];

                <FORM name=f>
                <INPUT type=text name=localtime>
                <script type="text/javascript"> 
                
                var now = new Date(); 
                
                var test = now.getFullYear() + '-' + 
                (now.getMonth() < 9 ? '0' : '') + (now.getMonth()+1) + '-' + 
                (now.getDate() < 10 ? '0' : '') + now.getDate() + ' ' + 
                (now.getHours() < 10 ? '0' : '') + now.getHours() + ':' + 
                (now.getMinutes() < 10 ? '0' : '') + now.getMinutes() + ':' + 
                (now.getSeconds() < 10 ? '0' : '') + now.getSeconds() 
                ; 
                f.localtime.value = test;  
                f.submit(); </script> </FORM>

                If you use a hidden field, then the user wont know the data is being collected.

                  This one includes Bjom ideas. Gets the users time and prints it out via PHP.

                  <?php
                  if ($_GET["localtime"])
                  {
                    $localtime = date("Y-m-d H:i:s", strtotime($_GET["localtime"]));
                    echo "<h1>$localtime</h1>";
                  } else {
                  ?>
                  <FORM name=f><INPUT type=hidden name=localtime>
                  <script type="text/javascript"> 
                  f.localtime.value = new Date().toLocaleString;  
                  f.submit(); </script> </FORM> <?php } ?>

                  There does seem to be a bug though. The date isn't converted correctly. Unless resolved, I'd format the time in JavaScript as you have done.

                    [removed] I apparently missed reading half the posts on this page.

                      Write a Reply...