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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STR_TO_DATE(str, format)

Status
Not open for further replies.

atsea

Technical User
Feb 27, 2005
51
JP
I have a form with a bunch of date fields (among others: name, address, etc)...

each date field accepts the date in the format mm/dd...

All the information on the form is saved to a DB...

My save (submit) feature works great except for the dates. Beacuse the format does not match the yyyy-mm-dd format. Currently dates are always stored as 0000-00-00.

Is there a way to format the date in the insert statement so that it can be stored properly in the DB?

Also, since the user cannot enter the year I would like to have the current year to be automatically entered. Is this possible?

I believe STR_TO_DATE is the answer, however I don't have anything working at the moment.

Thanks

atsea
 
How about:
[tt]
STR_TO_DATE(
CONCAT(YEAR(CURDATE()),'/',$monthday),
'%Y/%m/%d'
)
[/tt]
 
The input contains a slash, not a hyphen. There are of course other ways to do it, but that's one of the simplest, unless I'm missing something.
 
yeah, it's pretty wacky
mysql.com said:
You can specify DATETIME, DATE, and TIMESTAMP values using any of a common set of formats:
[ul][li]As a string in either 'YYYY-MM-DD HH:MM:SS' or 'YY-MM-DD HH:MM:SS' format. A "relaxed" syntax is allowed--any punctuation character may be used as the delimiter between date parts or time parts. For example, '98-12-31 11:30:45', '98.12.31 11+30+45', '98/12/31 11*30*45', and '98@12@31 11^30^45' are equivalent.[/li]
[li]As a string in either 'YYYY-MM-DD' or 'YY-MM-DD' format. A "relaxed" syntax is allowed here, too. For example, '98-12-31', '98.12.31', '98/12/31', and '98@12@31' are equivalent. [/li][/ul]
:)

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top