Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a Date to a MySQL Database

Status
Not open for further replies.
Joined
Aug 27, 2001
Messages
502
Location
US
I am frustrated at a seemingly simple task.

I have a date field (0000-00-00) in a mysql database. The user enters his/her birthday into three separate text boxes (month, day, & year). After validation, I want to combine these three values into one variable and add that variable to the database.

The validation works. I just can't seem to get the three values to combine into a format (unix timestamp) which the database will accept. I have tried various combinations of
Code:
mktime()
,
Code:
date()
, &
Code:
strtotime()
.

HELP!!

Frustrated,
-Ron [mad]

-We are all given the same deck of cards, it's how we play the hand we are dealt which makes us who we are.
 
I use this option before I insert the date

$year='2003';
$month='09';
$day='05';

Datenew=$year ."-". $month ."-" . $day;

but I just use the date format in mysql, timestamp format is for automatic updates when the record is changed?? but the format you descripe above isn't timestamp ??


 
To just be a bit more explicit:
Assemble your date from the individual parts following what sleipnir214 said. Use hyphen or forward slashes.

However, are you validating the input or are you trusting MySQL to turn someones "entered" birthday on November 31st into December 1st?
 
I am using the
Code:
mktime()
function to validate the date. If it is not a valid date, then it is not inserted into the database.

Using sleipnir214's method looks like it may work, however, how do I make sure that they are using DD-MM-YYYY instead of D-M-YY. For instance, 06-13-1970 instead of 6-13-70? Or, doesn't it really matter?

-We are all given the same deck of cards, it's how we play the hand we are dealt which makes us who we are.
 
Oh - I assumed you split the entries already.
Experience teaches that it is best to split into three disting select menus that offer choices for day (1-31), month, year. Assembling after the fact and not allowing user formatted input saves headaches and ensures proper values.
That's what I recommend.
 
I use this code to make sure that correct dates are made

Code:
<tr><td>Jaar</td><td><select name=&quot;jaar&quot;><option value=&quot;2003&quot; selected>2003
<option value=&quot;2004&quot;>2004
<option value=&quot;2005&quot;>2005
</select>
</td>
<td>Maand</td><td><select name=&quot;maand&quot;><option value=&quot;01&quot;>januari
<option value=&quot;02&quot;>februari
<option value=&quot;03&quot;>maart
<option value=&quot;04&quot;>april
<option value=&quot;05&quot;>mei
<option value=&quot;06&quot;>juni
<option value=&quot;07&quot;>juli
<option value=&quot;08&quot;>augustus
<option value=&quot;09&quot;>september
<option value=&quot;10&quot;>oktober
<option value=&quot;11&quot;>november
<option value=&quot;12&quot;>december
</select></td>

<td>Dag</td><td><select name=&quot;dag&quot;><option value=&quot;01&quot;>01
<option value=&quot;02&quot;>02
<option value=&quot;03&quot;>03
<option value=&quot;04&quot;>04
<option value=&quot;05&quot;>05
<option value=&quot;06&quot;>06
<option value=&quot;07&quot;>07
<option value=&quot;08&quot;>08
<option value=&quot;09&quot;>09
<option value=&quot;10&quot;>10
<option value=&quot;11&quot;>11
<option value=&quot;12&quot;>12
<option value=&quot;13&quot;>13
<option value=&quot;14&quot;>14
<option value=&quot;15&quot;>15
<option value=&quot;16&quot;>16
<option value=&quot;17&quot;>17
<option value=&quot;18&quot;>18
<option value=&quot;19&quot;>19
<option value=&quot;20&quot;>20
<option value=&quot;21&quot;>21
<option value=&quot;22&quot;>22
<option value=&quot;23&quot;>23
<option value=&quot;24&quot;>24
<option value=&quot;25&quot;>25
<option value=&quot;26&quot;>26
<option value=&quot;27&quot;>27
<option value=&quot;28&quot;>28
<option value=&quot;29&quot;>29
<option value=&quot;30&quot;>30
<option value=&quot;31&quot;>31
</select>

</td>

</tr>

sorry it's in dutch but I think you can work it out for your website

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top