943,703 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1606
  • PHP RSS
Jan 16th, 2009
0

PHP MySQL date question.

Expand Post »
New PHP person here..

I am setting up an event registration page. The users must select from two arrival dates for hotel and flights. from my database, I am able to populate a dropdown with the two dates.
PHP Syntax (Toggle Plain Text)
  1. "SELECT DATE_FORMAT(firstarrival, '%d %M %Y') as newfirsta, DATE_FORMAT(secondarrival, '%d %M %Y') as newseconda, DATE_FORMAT(firstdepart, '%d %M %Y') as newfirstd, DATE_FORMAT(seconddepart, '%d %M %Y') as newsecondd FROM testevent WHERE id = ".$id.""

I format the date to make it look nice (if i don't, it shows up as 0000-00-00). The problem is, when I try and insert the record, it shows up as zeros anyway.

Below is the snippet from the drop down. If i need to format the date back, should I do it here or what?

Thanks,

PHP Syntax (Toggle Plain Text)
  1. <label>Please Select Your Arrival Date:</label>
  2. <select name="arrivaldate" id="arrivaldate">
  3. <?php foreach ($traveldates as $traveldate) { ?>
  4. <option value="<?php echo htmlentities ($traveldate["newfirsta"]) ?>"><?php echo htmlentities ($traveldate["newfirsta"]) ?>
  5. <option value="<?php echo htmlentities ($traveldate["newseconda"]) ?>"><?php echo htmlentities ($traveldate["newseconda"]) ?> <?php } ?>
  6. </select>
Last edited by jzimmerman; Jan 16th, 2009 at 1:55 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jzimmerman is offline Offline
11 posts
since Jan 2009
Jan 16th, 2009
0

Re: PHP MySQL date question.

You'll want to format the date back prior to doing the insert. So, in the script you use to process the form data, use strtotime to convert the string to a timestamp. Then insert the data.
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Jan 16th, 2009
0

Re: PHP MySQL date question.

Thanks...so do I do that with the following line?
PHP Syntax (Toggle Plain Text)
  1. $arrivaldate = mysql_real_escape_string($_POST['arrivaldate']);
?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jzimmerman is offline Offline
11 posts
since Jan 2009
Jan 16th, 2009
0

Re: PHP MySQL date question.

Yeah, I'd say something like:
php Syntax (Toggle Plain Text)
  1. $arrivaldate = mysql_real_escape_string(strtotime($_POST['arrivaldate']));
would work. Try it out and post back.
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Jan 16th, 2009
0

Re: PHP MySQL date question.

Well I realized something while working on this. I am retrieving this and storing it in the database as a DATE. It came up with 0000-00-00 even with the last edit and that is what made me look around the database and remember that it is being stored in a date format. If that changes anything, my apologies for omitting that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jzimmerman is offline Offline
11 posts
since Jan 2009
Jan 16th, 2009
0

Re: PHP MySQL date question.

Ok, well try this:
php Syntax (Toggle Plain Text)
  1. $arrivaldate = mysql_real_escape_string(date("Y-m-d",strtotime($_POST['arrivaldate']));
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Jan 16th, 2009
0

Re: PHP MySQL date question.

Perfect!!! I was just about to try something more complicated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jzimmerman is offline Offline
11 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Msql. Insert data depending on which form field is filled out.
Next Thread in PHP Forum Timeline: PHP/Ajax search facility





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC