• t
  • g
  • B
  • Z
  • @
  • e

portfolio & blog of senior web developer, Fahd Murtaza



  • About
  • Portfolio
  • WordPress
  • CV
  • Blog
  • Contact

  • About
  • Portfolio
  • WordPress
  • CV
  • Blog
  • Contact

Convert date formats between PHP and MySQL (mm/dd/yyyy to yyyy-mm-dd)

September 8th, 2008 by Fahd

I have a view in HWC where I am using Javascript DHTML date picker script to pick the date and insert into test field. By the way HWC is the PHP-MVC (using codeigniter) application I am working on.

Date Picker in dhtml/Javascript

Date Picker in dhtml/Javascript

So as you see, the date pickers default format is mm/dd/yyyy,  which is not acceptable by MySQL date/time field as it is, so we need a little processing to make MySQL happy enough to store it ;)

Converting  mm/dd/yyyy to yyyy-mm-dd

$date=split("/",$_POST['date']);
// where the $_POST['date'] is a value posted by form in mm/dd/yy format
$dated=$date[2]."-".$date[0]."-".$date[1];
// The string dated is now in yyyy-mm-dd format
echo $dated;

For example, the date entered through form was 09/02/2009, after processing it will be stored in $dated as 2009-02-09. Pretty kool yeah? Just notice if you have / as I am happy if helps you. You need help with more similar stuff, post a comment.

Posted in CodeIgniter, PHP, Web Development

One Comment

  1. Alan
    Posted October 8, 2008 at 5:42 pm | Permalink

    If you are processing US or EUR dates for insert into MySQL AND if you’re using a newer version (4.1.1 or later), you also have the str_to_date function:

    US:
    SELECT str_to_date(’09/08/2008′,’%m/%d/%Y’)

    EUR:
    SELECT str_to_date(’09/08/2008′,’%d/%m/%Y’)

    I think postgresql and Oracle have a similar, “to_date” function, but the formatting strings use a different syntax.

Post a Comment

Click here to cancel reply.

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Categories


Want to Hire Me?


Fahd Murtaza is the guy behind fahdmurtaza.com: your source for getting your web development projects done. Fahd sepcialises in Open Source Web Development and his favorite is WordPress. This site is a platform to his web development portfolio and blog all on a newly designed responsive website—try viewing it on your mobile.

If after viewing Fahd's portfolio, you think he might be just the guy to hire for your web / application development; check out the web development pricing page, then find out how to hire Fahd with a web development project idea and a brief of requirements.

 


Fahd Murtaza has put together a number of Web Development Articles that will help you see what he does, how he does it. You will also be able to see the level of work, and passion, that Fahad puts into each and every job. Understanding a little about the process can make the idea of investing good money much more palatable. People have always appreciated my Agile techniques. Its time you

Fahd Murtaza, Web Developer, Programmer, Wordpress Expert
U
This is portfolio & blog of senior web developer Fahd Murtaza, who has 9 years experience in: website development, WordPress, drupal, CMS and CRM application development with passion of making web better; one site at a time.


@
Mobile +968 93 678 199
email info@fahdmurtaza.com
Google Talk: fahdim@gmail.com
Skype fahd.murtaza
Location Muscat, Oman.


_
Developed using my beloved, love of my life, WordPress, built on the responsive, grid based, mobile optimised, Foundation Framework, and a modified Foundation theme. More Info →


Follow Fahd: Twitter / Google+ / Instagram / Facebook / Dribbble / Tumblr / Posterous/ flickr /
Copyright © 2006-2012 Fahd Murtaza

    • WordPress.org
    • Documentation
    • Support Forums
    • Feedback