-1

I used a variation of Amsul datetime picker found at http://jsfiddle.net/amsul/h2V6w/

HTML code

    <h3><label for="call_time">Pick a date &amp; time:</label></h3>
    <div id="wrap">
        <input type="text" id="call_time"/>
        <div id="hidden">
            <input type="text" id="date"/>
            <input type="text" id="time"/>
        </div>

        <div id="outlet"></div>
</div>

I have this PHP code to get the date and time from the form above

<?php
    require("somedatabasedetails.php"); /*** mysql hostname info ***/

    if(isset($_POST['Email'])) 
    {
      try
      {
         // Some other codes here;  I used array here since I deleted other 
         //entries and elements that are working and irrelevant to my question   

         $call_time = $_POST['call_time'];
         $call_time = date('Y-m-d H:i:s', strtotime($call_time));

         $que=  "INSERT INTO emailsent(call_time) VALUES (:call_time)"; 

         $query2    = $dbh->prepare($que);

        $results  = $query2->execute(array(
            ":call_time"=>$call_time,
        ));
     }

        catch (PDOException $e)
      {
        $error = 'Error adding elements to database: ' . $e->getMessage();
        echo "Error: " . $error;
        exit();
      }

      exit();
    }
?>

On the mysql database, call_time column is formatted as datetime. I tried it also varchar just to test.

My problem is when $call_time is saved on the database it reveals this 1969-12-31 19:00:00. I have read and tried the queries made in these site, but i cant figure out how to get the user entered datetime and save it with the same datetime format to the database.

What part of the code did I do wrong? Or do i need to change timezone?

thanks from a newbie.

Andy
  • 15
  • 7
  • `varchar` datatype also didnt worked? – user2936213 Jan 03 '14 at 04:53
  • Well, what is the *result* of `date('Y-m-d H:i:s', strtotime($call_time));`? It is *not* what you are expecting. – user2864740 Jan 03 '14 at 04:58
  • I got 1969-12-31 19:00:00 when the value of $call_time is inserted on the database. – Andy Jan 03 '14 at 05:14
  • varchar did not work. Actually, when the a user used the datetime form, it will reveal "7 January, 2014 @ 1:00 PM." format which is likely generated by the script. But when sent to database it is inserted as 1969-12-31 19:00:00. try http://jsfiddle.net/amsul/h2V6w/. Thanks for reply – Andy Jan 03 '14 at 05:18
  • Set name for all input field in html – Awlad Liton Jan 03 '14 at 05:50
  • Where is the form ? i did not see any form in your html – Awlad Liton Jan 03 '14 at 05:51
  • @Awlad Liton: I think your correct. Of all the form fields although not included here, this one is the only one that has a different format (coz i followed the format as sampled on the link). That also explains the error when i used the suggestion of grebneke. How can I get the value then from the form when if used on the browser itself it showed the value on the field as sampled on the link? Thank you so much. – Andy Jan 03 '14 at 06:30
  • Now what error you are getting ? – Awlad Liton Jan 03 '14 at 06:32
  • See my errors as i explained it below to @grebneke. Thanks. I think my html form code may likely be lacking. May be the 'name' or 'for' attributes are lacking or misplaced. What do you think? But how can i have them all work together when the for attribute is dependent to the script? – Andy Jan 03 '14 at 06:57
  • @Andy : You should have name attribute for all input field for getting POST data. – Awlad Liton Jan 03 '14 at 08:09
  • Ok may i know why my question is a duplicate to the one that is linked? My question did not involved conversion into another format but primarily how the current format is saved differently on database. I have read the post beforehand but i knew it is not the answer I am looking for. – Andy Jan 04 '14 at 03:47

3 Answers3

1

Please add a line to check the value before inserting it in the database:

...
$call_time = date('Y-m-d H:i:s', strtotime($call_time));
// add this and check the output:
var_dump($call_time);

Is $call_time what you expect it to be at that point?

grebneke
  • 4,414
  • 17
  • 24
  • Thank you for your reply. It shows 2 things: 1. an error stating Undefined index: call_time ...; and 2. at the end of the first error "string '1969-12-31 19:00:00' (length=19). I never expected this. Can you explain this error? – Andy Jan 03 '14 at 06:13
  • Well, you're getting the wrong data from the html form, so it's not a database problem. Now, add a `var_dump($_POST['call_time']);` to see the actual data you're getting from the form. That's where the problem is, most likely. – grebneke Jan 03 '14 at 06:17
  • Your suggestion helped a lot in identifying some errors i made. I mentioned you in one of my answers above. Now, after using var_dump($_POST['call_time']); aside from the first error I mentioned before i got a NULL. Likely, the third error indicated is the result of undefined call_time; Error adding elements on database. Now, I'm getting somewhere. Thanks. I believe now there is something wrong with my HTML as where I should get the value of call_time. – Andy Jan 03 '14 at 06:44
  • I really want to vote up your answer but I am low in reputation. but many thanks. – Andy Jan 03 '14 at 07:38
0

you have to use this code

<input type="text" name="call_time" id="call_time"/>
Anil Meena
  • 903
  • 1
  • 12
  • 28
  • This does not work. If you check the link where i derived the html code, its functionality is dependent on some script it was made for. Thank you for the reply though. My main concern is the datetime i derived from the form and saved to the database. – Andy Jan 03 '14 at 05:12
  • Sorry I ignored your answer a bit, and thank you very much for it. After using the suggestions of the answers above, I figured your answer is half step in getting the answer i need. After using your code, I was able to get the value from the form and display it. What I just did after was change the column type of call_time in my database into varchar and I worked well. I was hoping though to save it as datetime type, but For now it suffices. Thanks to Awlad Liton and @grebneke. – Andy Jan 03 '14 at 07:28
  • May I suggest you edit your answer as i suggested above and i will check it as my accepted answer. Thanks again – Andy Jan 03 '14 at 07:34
0

fix HTML :

<h3><label for="call_time">Pick a date &amp; time:</label></h3>
    <div id="wrap">
        <input type="text" id="call_time" name="call_time"/>
        <div id="hidden">
            <input type="text" id="date"/>
            <input type="text" id="time"/>
        </div>

        <div id="outlet"></div>
</div>

It seems that:
Your $_POST['call_time'] has value like this:

   6 January, 2014 @ 1:00 AM

if so try this:

    $call_time =$_POST['call_time'];
    $arr = explode("@", $call_time);
    $call_time = date('Y-m-d H:i:s', strtotime($arr[0]));
    $que=  "INSERT INTO emailsent(call_time) VALUES (:call_time)";
    $query2    = $dbh->prepare($que);
    $results  = $query2->execute(array(
        ":call_time"=>$call_time,
    ));

If your $_POST['call_time'] value like this:

 6 January, 2014

then try your code:

     $call_time = $_POST['call_time'];
     $call_time = date('Y-m-d H:i:s', strtotime($call_time));

     $que=  "INSERT INTO emailsent(call_time) VALUES (:call_time)"; 

     $query2    = $dbh->prepare($que);

    $results  = $query2->execute(array(
        ":call_time"=>$call_time,
    ));
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53
  • Thank you so much. I have learned a lot. I did not however explode @ since it is understandable in the database and when echoed on the website. It teaches me nevertheless another technique. I noticed however if i used the 2nd one it rendered $call_time null again. It is not the reason I did not used it but it made me wonder why so. But my problem is already solved, i will figure it out later. – Andy Jan 04 '14 at 03:42