Insert into MySQL from PHP form text field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
murph2481
Forum Newbie
Posts: 20
Joined: Mon Jun 27, 2005 1:48 pm

Insert into MySQL from PHP form text field

Post by murph2481 »

So if i have a text box and then I want to capture that text box value as a date value and insert it into a MySQL database to a datetime type field how can i do that? Right now no value is captured i just get 0000/00/00.

Ideally if someone has a function that i can call that would format the $_POST['date'] value to what MySQL needs for the SQL statement that would be awesome.

Thanks in advance,
Dan
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

depending on the formats you wish to support (there are so so many) you may simply want to use strtotime() to get a unix timestamp, which you can give to MySQL's FROM_UNIXTIME()
murph2481
Forum Newbie
Posts: 20
Joined: Mon Jun 27, 2005 1:48 pm

Date

Post by murph2481 »

Actually even easier just change the format from 10/4/2005 to 2005-4-10 and it will work.

Anyone have a good parser to pull out each value (10,4,2005) and then change the order and the delimeter? Basically i want to change 10/4/2005 to 2005-10-4?

Thanks,
Dan
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

#Convert  10/4/2005 to 2005-4-10 

function dateConverter($date) {
   $array = explode('/',$date);
   $array = array_reverse($array);
   $array = implode('-',$array);
   return $array;
} 

echo dateConverter('10/4/2005');
echo 'Correct Value: 2005-4-10';

}
murph2481
Forum Newbie
Posts: 20
Joined: Mon Jun 27, 2005 1:48 pm

Perfect

Post by murph2481 »

Perfect thank you

Dan
:D
Post Reply