1

I want to insert two datetime's into mysql.

$taskcompdate is a user written string that needs to be converted into a datetime object and formatted with a "d/m/y" format.

The second, $datenow is simply the now time.

The insert query works, but all dates in all records are displayed as 0000-00-00 00:00:00

I tried every possible way I found, but none works. What am I doing wrong?

    <?php 
                $conn = mysqli_connect("127.0.0.1", "root", "", "todo_list");

                $taskname = $_POST["taskname"];
                $taskcompdate = $_POST["taskcompdate"];
                $datenow = date("H:i:s d/m/y");

                $insert_date1 = date('d/m/y', strtotime($taskcompdate));
                $insert_date2 = date('H:i:s d/m/y', strtotime($datenow));


                $sql_main = "INSERT INTO task_main (task, complete_date, added_date) VALUES ('$taskname', '$insert_date1', '$insert_date2')";
                $result = mysqli_query($conn, $sql_main);

if ($result) {
    echo 'success';
} else {
    echo 'failure' . mysqli_error($conn);
}
            ?>
2
  • can you let me know that what is the value of $insert_date1 and $insert_date2. Commented Oct 4, 2015 at 17:07
  • $insert_date1/2 is supposed to be a formatting and converting the strings into the format that is to be entered into the db. supposedly, but i just found some solutions online and tried it for me, but didn't work. Commented Oct 4, 2015 at 17:09

1 Answer 1

3

Your dates are in an invalid format for MySQL's date and datetime types. They must be in YYYY-MM-DD HH:MM::SS format.

$insert_date1 = date_format($taskcompdate, "Y-m-d");
$insert_date2 = date('Y-m-d H:i:s', strtotime($datenow));
Sign up to request clarification or add additional context in comments.

1 Comment

Great, thanks - it works. Can I use the same format you provided for just a date? Because I need the first to be just a date and the second a datetime?

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.