Forums
This topic is locked
Adding dates to MySQL with Dreamweaver
Posted 17 Mar 2007 01:57:36
1
has voted
17 Mar 2007 01:57:36 Alexis Lalas posted:
hello to all.im inserting information into a mysql database and i one is the date and i insert it into my DB with the date format but im getting it from the form in 3 different menu/lists, day, month and year.
how can i get them together into one variable so i can insert it into the DB? because ive been trying several ways and all i get is 0000/00/00 or just the day and the month and not the year.
thanks and have a nice day!
Replies
Replied 19 Mar 2007 18:29:26
19 Mar 2007 18:29:26 Alan C replied:
HI
You have to assemble the three parts of the date together and get them into the right format to go into the table, here is the function I wrote to do it, you may have to modify it to suit your requirments . . .
<pre id=code><font face=courier size=2 id=code>function assemble_date($dd, $mm, $yyyy)
{
# takes the three parts of the date from the form input and returns an iso formatted date
# ready for mysql to insert into the table
# cleanup consists of removing anything except numbers and limiting length
# padding zeros are added when necessary
# final format will be YYYYMMDD
#
# Alan C May 2006
$regex = '[^0-9]'; // means anything except digits 0 thru 9
$yyyy = preg_replace($regex,'',$yyyy);
if(strlen($yyyy)<4) {$yyyy = '0000';} // something wrong, less than 4 digits entered for year
$mm = preg_replace($regex,'',$mm);
$mm = str_pad($mm, 2, '0', STR_PAD_LEFT);
$dd = preg_replace($regex,'',$dd);
$dd = str_pad($dd, 2, '0', STR_PAD_LEFT);
return $yyyy.$mm.$dd;
} </font id=code></pre id=code>
the tidy-up mechanism is needed because you don't know what characters other than 0-9 users will put in the textfield. On my form I have yyyy mm dd as preset text in the fields so that people know what to put, it also enables me to look at the input and check that they have actually put something in there.
You have to assemble the three parts of the date together and get them into the right format to go into the table, here is the function I wrote to do it, you may have to modify it to suit your requirments . . .
<pre id=code><font face=courier size=2 id=code>function assemble_date($dd, $mm, $yyyy)
{
# takes the three parts of the date from the form input and returns an iso formatted date
# ready for mysql to insert into the table
# cleanup consists of removing anything except numbers and limiting length
# padding zeros are added when necessary
# final format will be YYYYMMDD
#
# Alan C May 2006
$regex = '[^0-9]'; // means anything except digits 0 thru 9
$yyyy = preg_replace($regex,'',$yyyy);
if(strlen($yyyy)<4) {$yyyy = '0000';} // something wrong, less than 4 digits entered for year
$mm = preg_replace($regex,'',$mm);
$mm = str_pad($mm, 2, '0', STR_PAD_LEFT);
$dd = preg_replace($regex,'',$dd);
$dd = str_pad($dd, 2, '0', STR_PAD_LEFT);
return $yyyy.$mm.$dd;
} </font id=code></pre id=code>
the tidy-up mechanism is needed because you don't know what characters other than 0-9 users will put in the textfield. On my form I have yyyy mm dd as preset text in the fields so that people know what to put, it also enables me to look at the input and check that they have actually put something in there.