Take date from HTML5 form - and Post to MySQL

15,999

You need to use strtotime() before calling date(), so you can work on UNIX timestamps (which are required for date ;). Plus the indexes aren't matching the ones defined in the HTML.

$start = date("Y-m-d", strtotime($_GET['order_contractStart']));
$end = date("Y-m-d", strtotime($_GET['order_contractEnd']));

Also don't be fooled by w3schools!

Share:
15,999
Lorienas
Author by

Lorienas

Updated on June 04, 2022

Comments

  • Lorienas
    Lorienas almost 2 years

    I've got a basic web form, and I need to take a date from a "date" type input (html5 see http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_input_type_date) and post it into my MySQL database (which has a type of DATE - note: not DATETIME).

    I need some kind of PHP/JS voodoo to make the 2 talk to each other, Any clues?

    Heres what I've got so far, and it always produces the default date of 1970-01-01.

    Would it be better to do it as a plain input and force the format with Jquery?

    <!-- zee form: -->
    <div>
        <!-- Start Date -->
        <div>
            <label class="col-offset-half col-3" for="order_contractStart">Contract Start:</label>
            <input class="col-offset-half col-7" type="date" id="order_contractStart" name="order_contractStart">
        </div>
        <!-- End Date -->
        <div>
            <label class="col-offset-half col-3" for="order_contractEnd">Contract End:</label>
            <input class="col-offset-half col-7" type="date" id="order_contractEnd" name="order_contractEnd">
        </div>
    

    Then in my action I have:

    //Get Variables from form
    $start = date("Y-m-d", $_GET['order_start']);
    $end = date("Y-m-d", $_GET['order_end']);
    

    Turns out this method isn't really working - so any pointers would be greatly appreciated.

    • Ronni Skansing
      Ronni Skansing over 10 years
      you are using "order_contractStart" in the name attribute, make sure you use the same in php, $_GET['order_contractStart']. You also need a form tag around the input
  • Lorienas
    Lorienas over 10 years
    So its still a string, I never would have thought. Its working just fine now. I had already changed the indexes - but it was still baffling me. Annoying that I've just found out that html5 date doesn't work in safari.
  • Nico Haase
    Nico Haase over 2 years
    What do you mean by that? PLease add some explanation to your answer such that others can learn from it