Nostradonis
Nostradonis

Reputation: 55

I'm trying to convert a text string to a date for a mySQL database via PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="author" content="James McInnis">

    <img src="../images/paper.jpg" class="bg">
    <link href="../css/main.css" rel="stylesheet" type="text/css">
    <ul class="nav">
        <li><a href="../html/home.html">Home</a></li>
        <li><a href="../html/person.html">Person</a></li>
        <li><a href="../html/contacts.html">Contacts</a></li>
        <li><a href="../html/events.html">Events</a></li>
        <li><a href="../html/planner.html">Planner</a></li>
    </ul>

</head>

<body>

    <div id="content">
        <p id=para2>Person</p> 
        <Center><p id=para1>Here is where you can add yourself or another person to the pool of available people. Once you're in the pool of available people, you can create a planner and add contacts to it. <br><br><br>ADD LINK TO QUERY PEOPLE HERE.<br><br><br></p>  

        <form action="../php/newConnect.php" method="post"><center>
            Personal Identifier:<br>
            <input type="text" name="Person_ID">
            <br>
            <center>First Name:<br>
            <input type="text" name="First_Name">
            <center> Last Name:<br>
            <input type="text" name="Last_Name">
            <center> Sex:<br>
            <select name="Sex">
            <option value="Male">Male</option>
                <option value="Female">Female</option>
            </select>
            <!-- <input type="text" name="Sex"> -->
            <center> Date of Birth:<br>
            <input type="text" name="Date_Of_Birth">

            <center><input type="submit">
        </form>

    </div>

</body>

</html>

The database we're using has Date of Birth stored in the date type in the format YYYY-MM-DD. Dates are posting to the DB, but they're all 1970-01-01 which I'm guessing is the default. Am I using the strtontime or strtodate functions incorrectly? Or should I change the input type on the HTML side?

<!DOCTYPE html>
<html>
<body>

<?php
$conn=mysqli_connect("localhost", "james", "QzVHdN", "james_PlannerDB");

// Check connection
if (mysqli_connect_errno()) {
     echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

if (isset($_POST['Person_ID']) && isset($_POST['First_Name']) && isset($_POST['Last_Name']) && isset($_POST['Sex']) && isset($_POST['Date_Of_Birth'])){

    $Person_ID = $_POST["Person_ID"];
    $First_Name = $_POST["First_Name"];
    $Last_Name = $_POST["Last_Name"];
    $Sex = $_POST["Sex"];
    $Date_Of_Birth = $_POST[$Date_Of_Birth];
    $Date_Of_Birth = date('Y-m-d',strtotime(str_replace('/','-',$Date_Of_Birth)));






    $statement = "INSERT INTO PERSON(Person_ID, First_Name, Last_Name, Sex, Date_Of_Birth) VALUES ('" . $Person_ID . "','" . $First_Name . "' , '" . $Last_Name . "', '" . $Sex . "','" . $Date_Of_Birth . "')";     

    if ($conn->query($statement) === TRUE) {
          echo 'Person added!';
        } else {
          echo 'Problem detected';
        }
    } else {
    echo 'Missing parameters';

    }       
    mysqli_close($conn);

?>  

</body>
</html>

Upvotes: 0

Views: 41

Answers (3)

Quickpick
Quickpick

Reputation: 191

You need not to replace the 'dashes' with slashes. strtotime is pretty good at reading human date format. YYYY-MM-DD should be good.

Try...

...
$Date_Of_Birth = date('Y-m-d',strtotime($Date_Of_Birth));
...

Upvotes: 0

Devon Bessemer
Devon Bessemer

Reputation: 35337

PHP handles dates with both / and -. If you use a - PHP will interpret dates in DD-MM-YYYY as opposed to MM/DD/YYYY. So judging by your code, you'd need to have all inputs be in a DD-MM-YYYY or YYYY-MM-DD format in order for strtotime() to properly interpret it.

However, it looks like Chris has pointed out a fault in your code before it even gets to this point.

Upvotes: 2

Chris Trudeau
Chris Trudeau

Reputation: 1437

Try changing:

$Date_Of_Birth = $_POST[$Date_Of_Birth];

to:

$Date_Of_Birth = $_POST["Date_Of_Birth"];

Right now you're trying to set $Date_Of_Birth to the value of the $_POST array located at $Date_Of_Birth, which doesn't really make any sense :)

Upvotes: 1

Related Questions