Web Design & Internet Marketing
Text or Call Now: 813.360.0932


HOW TO convert date from a form to mysql format in php

kimfooyoung

One problem I have come across many times is accepting dates into a form field (perhaps on a cms backend) and needing to format it to another order or date layout to enter into mysql. Commonly since mysql accepts yyyy-mm-dd and I accept dates as mm-dd-yyyy the format needs to be changed from mm-dd-yyyy to yyyy-mm-dd in order for mysql to take it properly. Simply take $yourdate and convert it to $fdate with the php code below. You can change the array number order if you wanted to do something different.

 
To modify this for your purposes you will need to know what it is doing. First it is taking your dates and removing the pieces between the - in the explode function and then placing them into an array, reordering the array and imploding it (replacing it into a string) into the $fdate variable. Make sense? Any questions you have you can leave in the comments.
 
  1. $date = explode("-",$yourdate);
  2. if ($date[0]<=9) { $date[0]="0".$date[0]; }
  3. if ($date[1]<=9) { $date[1]="0".$date[1]; }
  4. $date = array($date[2], $date[1], $date[0]);
  5. $fdate=implode("-", $date);


We thought you might like to see these other posts about web development...



kimfooyoung About The Author
Kim Young is Founder, CEO, and a developer at Foo - a web development company based in Wesley Chapel, Florida. She can also be found on . With over 16 years of experience as a web developer, Kim is excited to share with you tidbits that she has picked up along the way. Kim prioritizes continuing education and out of box thinking in order to bring the most valuable solution to Fooweb's clients.

SHARE WITH YOUR FRIENDS


Let's Do This!


We are so excited to hear more about your organization and chat about how we can drive more traffic your way. Reach out to us now and get a free quote.

Request Pricing Now


web development services© Foo Web Development LLC    Web Design & Development    Serving Clients Worldwide    Client Login   RSS Feed   Feedburner