Small date issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Small date issue

Post by Crashin »

I've read the manual on php.net and evidently I'm too tired to get it right now. I've got a user form where the user enters a date in mm/dd/yyyy format. How do I convert that to the format that can be written to my MySQL table, which accepts yyyy/mm/dd? Thanks! :oops:
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Why not match your form date format, to your MySQL format.
Image Image
User avatar
pHaZed
Forum Commoner
Posts: 28
Joined: Wed May 01, 2002 2:44 am
Location: Sydney -AU

Current Date?

Post by pHaZed »

If you are trying to retrieve todays date,
just use the date() function.
http://www.php.net search for date in functions,
Otherwise if you want watever date the user enters to be recorded...
It should insert simply as a string, If you make the table type LONGTEXT... etc
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

split the input-field into three: 'day' 'month' and 'year' so you can assemble it as you wish

or rearrange the value of the input-field withsubstr
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Code: Select all

list ($month, $day, $year) = split ('ї/.-]', $StartDate);
         $StartDate = date('Y-m-d',mktime(0,0,0,$month,$day,$year));
$StartDate is the variable passed into your script.

This will cope with the following input mm/dd/yyyy mm-dd-yyyy or mm.dd.yyyy

Hope this helps
Mike
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

Fantastic suggestions! Thank you all so much! I'll start going through them to find the best fit! This forum is the best! :D
IndyTim
Forum Newbie
Posts: 2
Joined: Thu May 02, 2002 2:49 pm
Location: Indy,Indy(Indianapolis Area)

Post by IndyTim »

Something else to consider, once you've settled in on a date conversion, you might want to build it (and other related date conversions etc.) into a user callable function. That way you'll get heavy re-use as your scripting library increases. I have to deal with conversions from MySQL to Unix to_fro EvilEmpire Short Dates etc. Callable functions definitely simplify things.

IndyTim
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

That's a great idea, indy! Thanks for the tip!
Post Reply