1

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.

1
  • 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 Commented Nov 21, 2013 at 22:02

3 Answers 3

5

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!

Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

if you use

<input type="date" name="DOB">

then

$DOB = $_POST['DOB'];

as if you echo

echo $_POST['DOB'] . '<br>';
echo strtotime($_POST['DOB']) . '<br>';
echo date("Y-m-d", strtotime($_POST['DOB'])) . '<br>';

you will see that the 1st and 3rd line are the same. My problem was that I was using Bind SQL, which needs to be

$stmt->bind_param('s

with an 's' and also structure for the column in the database set to Date.

Also <input type="date"> only works for Chrome, Safari and Opera, not Firefox or Internet Explorer. So I would recommend using something like

<input type="date" name="DOB" placeholder="dd-mm-yyyy" pattern="\d{1,2}-\d{1,2}-\d{4}">

for it to work on all browsers and for validation

Comments

0

this is finish

<!DOCTYPE html>
<html>
<body>
<!-- zee form: -->
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<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>
    <div><input type="submit"></div>

</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {

$start =  $_POST['order_contractStart'];
$end = $_POST['order_contractEnd'];
echo $start;
echo "<br>";
echo $end;

}
?>

</body>
</html>

1 Comment

What do you mean by that? PLease add some explanation to your answer such that others can learn from it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.