Jump to content

Submitting a date to Database from datepicker


popcop

Recommended Posts

ive not done anything really i just create the row for date am trying to send it from the form

 

ive added print_r($_POST);  to my page to see whats actually being send over and this is the resuts

 

Array ( [firstname] => tom [lastname] => mckay => tom@gmail.com [date] => 07/20/2011 ) Database updated with:tommckaytom@gmail.com

 

do i need to change anything in the actually DB??  or from the form??

 

Im actually wanting the date to be sent in this format for example  Saturday 30th July 2011  so that i can pull if from the database and display it on the page in that format

Link to comment
Share on other sites

The mysql DATE type exists for a few reasons. The format used is YYYY-MM-DD (or YYYYMMDD when treated as a number) because that format permits dates to be sorted and compared using greater-than/less-than comparisons. The DATE type also has the minimum storage requirements and it allows you to use a few dozen built-in date functions to operate on dates directly in your queries.

 

You need to get any submitted date into the YYYY-MM-DD format when you put it into a query. In your case, you can get the datepicker to do this for you. You should always validate all submitted data to insure that it is what you expect, in case someone is feeding your script unexpected data in an attempt to trigger errors or to break into your script or database.

 

You can format a DATE type any way you want when you retrieve it by using the mysql DATE_FORMAT() function in your query.

Link to comment
Share on other sites

should my field type be DATE in the database?

 

im gettina  little confused.

 

this is the return of whats being send to the DB from the form

Array ( [firstname] => hhh [lastname] => lll => gerrymckay@live.com [date] => 08/25/2011 ) Database updated with:hhhlllgerrymckay@live.com

 

from what youve said, i need to change the format to YYYYMMDD before the database will accept it.

 

im not 100% sure where to change this

Link to comment
Share on other sites

When you post the page like you said your date is coming back as 07/20/2011, it needs to come back as 2011-07-20 but that does not look nice for the user. So thankfully we are using php and there are built in date functions. You can do the following:

 

//get the correct format
$new_date = date('Y-m-d',strtotime($_POST['date']));

//then you can insert that date
$insert = 'INSERT INTO some_table (field1) VALUES("'.mysql_real_escape_string($new_date).'")';

 

Just as a note in case you dont know the strtotime function will take a date and return you a timestamp. Using the date function the first argument is format the date should be in and the second argument is a timestamp that you want formatted to the first argument. Hopefully this helps.

Link to comment
Share on other sites

  • 2 years later...

When you post the page like you said your date is coming back as 07/20/2011, it needs to come back as 2011-07-20 but that does not look nice for the user. So thankfully we are using php and there are built in date functions. You can do the following:

 

//get the correct format
$new_date = date('Y-m-d',strtotime($_POST['date']));

//then you can insert that date
$insert = 'INSERT INTO some_table (field1) VALUES("'.mysql_real_escape_string($new_date).'")';
Just as a note in case you dont know the strtotime function will take a date and return you a timestamp. Using the date function the first argument is format the date should be in and the second argument is a timestamp that you want formatted to the first argument. Hopefully this helps.

 

 

What do u mean with INSERT INTO some_table (field1) ?

 

//get the correct format

$new_date = date('Y-m-d',strtotime($_POST['date']));

 

//then you can insert that date

$insert = 'INSERT INTO some_table (field1) VALUES("'.mysql_real_escape_string($new_date).'")';

 

Can you give explain what i have to type in the "some_table(field) ?

 

Where does the date come from ($_POST['date'] ?

$new_date = date('Y-m-d',strtotime($_POST['date']));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.