Current location: Hot Scripts Forums » Programming Languages » PHP » Trouble inserting date into a field


Trouble inserting date into a field

Reply
  #1 (permalink)  
Old 05-26-04, 07:35 PM
digitalje5u5 digitalje5u5 is offline
Newbie Coder
 
Join Date: May 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Trouble inserting date into a field

I am wanting to convert a date that one enters in a form into a date field in a table. When the field is set as a date, it does not want to accept the date from the form. If I set the type to varchar, etc it works.

I am pretty sure this is a formatting error, but I am lost as how to go about this.

As it stands, I have the date format as 5/24/2004 in the form. How would I strip the "/" and reformat the input so that mySQL understands this?

THanks,

/DJ
Reply With Quote
  #2 (permalink)  
Old 05-26-04, 07:40 PM
dawggy505 dawggy505 is offline
Newbie Coder
 
Join Date: May 2004
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by digitalje5u5
I am wanting to convert a date that one enters in a form into a date field in a table. When the field is set as a date, it does not want to accept the date from the form. If I set the type to varchar, etc it works.

I am pretty sure this is a formatting error, but I am lost as how to go about this.

As it stands, I have the date format as 5/24/2004 in the form. How would I strip the "/" and reformat the input so that mySQL understands this?

THanks,

/DJ
i would use the ASCII character set (ex. a space is %20)
Reply With Quote
  #3 (permalink)  
Old 05-26-04, 07:47 PM
digitalje5u5 digitalje5u5 is offline
Newbie Coder
 
Join Date: May 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by dawggy505
i would use the ASCII character set (ex. a space is %20)
care to elaborate?

/DJ
Reply With Quote
  #4 (permalink)  
Old 05-29-04, 05:56 AM
digitalje5u5 digitalje5u5 is offline
Newbie Coder
 
Join Date: May 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
bump the wax tadpole
Reply With Quote
  #5 (permalink)  
Old 05-29-04, 03:53 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

MySQL Date type accepts a string like "2004-05-28" if I recall correctly. So if your input is like "05/28/2004", you can convert this into the correct format.

You can do either of the following (or more):

1) explode() it with "/" and build the correct date format,
2) substr() to extract each component and glue them together, or
3) strtotime() it to onvert it first to UNIX timestamp and use date() to make the correct format out of it.

If you need an example, you can visit http://wwwphp.net and type in the function of your choice. Good luck then!
__________________
Blavv =|
Reply With Quote
  #6 (permalink)  
Old 05-29-04, 05:37 PM
cheesehead cheesehead is offline
New Member
 
Join Date: May 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by digitalje5u5
I am wanting to convert a date that one enters in a form into a date field in a table. When the field is set as a date, it does not want to accept the date from the form. If I set the type to varchar, etc it works.

I am pretty sure this is a formatting error, but I am lost as how to go about this.

As it stands, I have the date format as 5/24/2004 in the form. How would I strip the "/" and reformat the input so that mySQL understands this?

THanks,

/DJ
You can use strtotime function of PHP to convert the date from the form field to an internal UNIX time. Then store that time into an Integer field.

int strtotime ( string time [, int now])

<?php
$str = 'Not Good';
if (($timestamp = strtotime($str)) === -1) {
echo "The string ($str) is bogus";
} else {
echo "$str == ". date('l dS of F Y h:i:s A',$timestamp);
}
?>

When you want to format the internal date back to a formatted date to output to the html page then use the "date" function.

Hope this helps.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
retieveing selected date from a calendar popup using javascript into datetext field yusufoladele JavaScript 1 11-27-06 04:43 AM
formmail problem gscraper Perl 12 08-27-04 03:06 AM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM
URRRGGGG!! - Still having DATE trouble tcooper PHP 3 12-18-03 05:13 PM
Checkbox depending on another field. How ? pravetz ASP 4 10-07-03 05:36 PM


All times are GMT -5. The time now is 10:36 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.