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

Date format issue

Status
Not open for further replies.

shamrox

Programmer
Sep 27, 2001
81
US
I have a standard form on a webpage that has a field for the date. I'm using php/mysql.
I want my users to be able to key in the date in the format of MM/DD/YYYY. If they do it now, it gets submitted into the database as YYYY-MM-DD, but the numbers don't get transposed properly so if they had keyed 08/11/2004 it goes in as 2008-11-20. This is a problem.
So, my question is, how do I get it to submit into the database properly???

 
i think nothing can be done about that, a database stores data in its own format, when u retrieve the value in a php file does it display in the correct format???

Known is handfull, Unknown is worldfull
 
Actually, cLFlaVA provided an answer when this question was posted in the MySQL forum in thread436-897592:

cLFlaVA said:
Code:
$the_date = date( "Y-m-d", strtotime($the_date));

This will be employed to transform the user-entered date string, then the new string will be used to build a query that will be passed to MySQL


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
shamrox

That is the solution.

$the_date = date( "Y-m-d", strtotime($the_date));

Where do you put it?
I'm not sure what you're code looks like, but I'll assume you have something similar to this:

$sql = "INSERT INTO table_name VALUES(..., '$the_date', ...)";

Put the line of code I provided you on the line before this.

Or, if you don't know what I'm talking about, please post your code.

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
A general comment and suggestions:

a) I recommend never to rely on user input. You can't trust the user to enter the date in the format you ask for. Splitting the date components in drop down menus that are prepopulated will help assure proper input. Yes, you might get February 31 but that can be prevented with some smart client side scripting. Still a lot better than 24/56/5671

b) assemble the string YYYY-MM-DD according to International Standard ISO 8601 ( you might not have to use the provided strtotime function). Check it out.

c) Be kind. Competent free advise is hard to find these days.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top