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

Form - Date formats for php and mysql

Status
Not open for further replies.
Aug 23, 2004
174
US
I am working on a site for local bands and I want the bands to be able to enter their concert listings onto the site. I am trying to figure out the best way to enter the dates. As of now the only way I have found to do it is to have something next to the field to indicate to enter the date in yyyy-mm-dd. I would like the date to be able to be entered as mm-dd-yyyy on the form.

Is this possible?

Thanks.
 
You can create a mask using Javascript which would then put this outside the scope of this forum, or you could produce 3 text boxes instead of only one, each only accepting the desired information.


Example:

Code:
<input type=text size=2 maxlength=2 name="date_month">
<input type=text size=2 maxlength=2 name="date_day">
<input type=text size=4 maxlength=4 name="date_year">

In any case, the way the information is entered in the form, has little relevance on how you handle it afterwards. You could just as simply take the input and re arrange it to match your needs.

Although having 3 separate boxes, makes it easier to know what they are entering.
and of course you should never blindly trust what the user inputs, and verify it before actually using it.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
have them enter the date in mm-dd-yyyy - that's not a problem.

then to convert it to/from use in db use

Code:
function displaytodb($date){
 return date('Y-m-d', strtotime($date));
}
function dbtodisplay($date){
 return date('m-d-Y', strtotime($date));
}
 
Thanks for the replies.

I am having some trouble with strtotime. I enter a date as 9-25-2007 and it shows up as 2016-06-29.

I found this function to take any format and convert it to yyyy-mm-dd but I haven't tried it out yet.
I think I am going to go with vacunitas solution and just use three seperate input fields. I think it would be the most simple method and it is easy for users to understand.
 
my apologies. this code will work

Code:
function displaytodb($date){
 return date('Y-m-d', strtotime(str_replace('-','/',$date)));
}
function dbtodisplay($date){
 return date('m-d-Y', strtotime($date));
}

three boxes are great but even better is a javascript date picker. most of these can deliver a database format date to a hidden form for you to use in your php.
 
I decided to go with 3 select boxes just for ease of use.

I have all of the dates stored in my database, and now I want to display this information in calendar format on another page. I don't even have a clue how to go about this. Any recomendations?
 
I want to have a calendar of events. Basically just a normal calendar with all of the dates and events displayed for the current month. Also I would probably need some way to display calendars for other months.
 
given the year and month,
find the first day of the month and the last.
work out from those data points how many padding "days" you need to add at the beginning and end of your calendar
for each "real" day, query your database for the events and output them
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top