0

I have two inputs Time and Date. I want to convert these fields to one using php and insert them into a datetime field in mysql. I think I need to use a STR_TO_DATE. But I'm unsure how to do it. Thanks

Format Time = 12:00 PM
Date = 2010-11-17

5 Answers 5

1

Since you plan to use PHP, you can directly set into a format that MYSQL will accept,
such as

$the_date = date('Y-m-d H:i:s', strtotime($date.' '.$time);
$sql = "INSERT INTO YOUR_TABLE SET COL_FOR_DATE_TIME='{$the_date}'";
Sign up to request clarification or add additional context in comments.

Comments

0
$Time = "12:00 PM";
$Date = "2010-11-17";
$DateTime = $Date . " " . $Time;

$timestamp = date('Y-m-d H:i:s',strtotime($DateTime));

Comments

0

This PHP code to get a start and end date:

$datef = (date("Y-m-d H-i-s",mktime($_POST['hour'], 0, 0, $_POST['month'], $_POST['day'], $_POST['year'])));
$datel = (date("Y-m-d H-i-s",mktime($_POST['hour1'], 0, 0, $_POST['month1'], $_POST['day1'], $_POST['year1'])));

This was posted from a load of html dropdowns!

I know this doesnt exactly answer your question but it will give you a good foundation!

Comments

0

You should use a timestamp, it's a much more easily manipulated standard with just as much granularity as you need for your current setup.

As for "Converting" the table, that's pretty much impossible, you would need to make a new table, move the stuff over, drop the old one and rename the new one...

2 Comments

Timestamp does not work this is not a by entry form its based off the premise of input fields and people filling them out Timestamp automatically generates a time and date but the time and date the entry is put into the database is not what Im looking for. Also Im not trying to convert the entire table im converting input fields using php to then put data into a mysql database.
Timestamp is a format, you can take user input and put it in a timestamp and without the problems you will get without it... The way you are doing it is like storing a users first and last names in the same field because you can't be bothered looking at the benefits of splitting them up.
0

The STR_TO_DATE function is indeed a good option.

You could try something like this:

STR_TO_DATE('2010-11-17 12:00 PM', '%Y-%m-%d %h:%i %p')

2 Comments

since I have input variables how would I use them inside STR_TO_DATE?
If you're using php, like this: $query = "INSERT INTO table (date) VALUES(STR_TO_DATE('" . $date . "', '%Y-%m-%d %h:%i %p');"

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.