PHP MySQL date question.

Thread Solved

Join Date: Jan 2009
Posts: 11
Reputation: jzimmerman is an unknown quantity at this point 
Solved Threads: 0
jzimmerman jzimmerman is offline Offline
Newbie Poster

PHP MySQL date question.

 
0
  #1
Jan 16th, 2009
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.
  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,

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

Re: PHP MySQL date question.

 
0
  #2
Jan 16th, 2009
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.
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 11
Reputation: jzimmerman is an unknown quantity at this point 
Solved Threads: 0
jzimmerman jzimmerman is offline Offline
Newbie Poster

Re: PHP MySQL date question.

 
0
  #3
Jan 16th, 2009
Thanks...so do I do that with the following line?
  1. $arrivaldate = mysql_real_escape_string($_POST['arrivaldate']);
?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

Re: PHP MySQL date question.

 
0
  #4
Jan 16th, 2009
Yeah, I'd say something like:
  1. $arrivaldate = mysql_real_escape_string(strtotime($_POST['arrivaldate']));
would work. Try it out and post back.
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 11
Reputation: jzimmerman is an unknown quantity at this point 
Solved Threads: 0
jzimmerman jzimmerman is offline Offline
Newbie Poster

Re: PHP MySQL date question.

 
0
  #5
Jan 16th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

Re: PHP MySQL date question.

 
0
  #6
Jan 16th, 2009
Ok, well try this:
  1. $arrivaldate = mysql_real_escape_string(date("Y-m-d",strtotime($_POST['arrivaldate']));
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 11
Reputation: jzimmerman is an unknown quantity at this point 
Solved Threads: 0
jzimmerman jzimmerman is offline Offline
Newbie Poster

Re: PHP MySQL date question.

 
0
  #7
Jan 16th, 2009
Perfect!!! I was just about to try something more complicated.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1052 | Replies: 6
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC