7 replies
Here's my problem...

I'm trying to have users input their birthday via an html form in a particular format: DD-MM-YYYY

Then I want to INSERT that into my DB under the column: birthdate.

It seems that MySQL wants dates formatted in the YYYY-MM-DD format. How to I collect that information as a $string, and then convert it into the correct format?

I admit, I'm a novice at PHP and MySQL...this one has stumped me.

Anyone care to help?
#date #issue #php or mysql
  • Profile picture of the author SmartWeb
    I can help.
    Is your html form ready to take the date? or that also needs to be created ?
    Give me your html code i will add the php into that.
    {{ DiscussionBoard.errors[2875606].message }}
  • Profile picture of the author CrhisD
    Originally Posted by Big Squid View Post

    Here's my problem...

    I'm trying to have users input their birthday via an html form in a particular format: DD-MM-YYYY

    Then I want to INSERT that into my DB under the column: birthdate.

    It seems that MySQL wants dates formatted in the YYYY-MM-DD format. How to I collect that information as a , and then convert it into the correct format?

    I admit, I'm a novice at PHP and MySQL...this one has stumped me.

    Anyone care to help?
    Have three fields (month, date and year). After that you can concatenate it any way you want
    {{ DiscussionBoard.errors[2875616].message }}
    • Profile picture of the author Big Squid
      Thanks - That works fine...
      {{ DiscussionBoard.errors[2877821].message }}
      • Profile picture of the author mywebwork
        Originally Posted by CrhisD View Post

        Have three fields (month, date and year). After that you can concatenate it any way you want
        Originally Posted by Big Squid View Post

        Thanks - That works fine...
        It will work, but keep in mind that if your application is going to be used by real users in the real world you'll have to do some input conditioning.

        I assume from the comments you're simply taking the value of 3 textboxes and using them to create a string that conforms to the MySQL Date format? What happens if:

        - I enter a value above 12 for month or above 31 for day?
        - If I enter an illegal date, such as April 31st?
        - If I put text values in instead of numbers?
        - The two-digit vs 4-digit year issue (remember the Y2K problem?)
        - If I use a single digit for the numbers 0-9 - the MySQL Date format requires a leading zero in these cases.

        Just a thought - there are literally hundreds of ways to resolve this very common problem, using JavaScript to check inputs or (even better) to implement a datepicker control are the usual solutions.

        Bill
        {{ DiscussionBoard.errors[2878420].message }}
        • Profile picture of the author mojojuju
          Provided that the date from the form submit has been validated and is in the correct format, you might do it like this:

          $date_from_form = '17-11-2010';

          $date_for_mysql = implode('-', array_reverse(explode('-', $date_from_form)));

          ~~ Or like this: ~~

          $date_from_form = '17-11-2010';

          $date_for_mysql = date_format(date_create_from_format('d-m-Y', $date_from_form), 'Y-m-d');
          Signature

          :)

          {{ DiscussionBoard.errors[2879274].message }}
          • Profile picture of the author bort27
            The above date_create_from_format() function will only work if you're using PHP 5.3 or later. If you've got an older version, you can just do:

            $date_for_mysql = date('Y-m-d', strtotime($date_from_form));

            Or, you could make the DB figure it out:

            mysql_query(sprintf("INSERT INTO `yourtable` SET `birthdate` = FROM_UNIXTIME(%u)", strtotime($date_from_form)));
            {{ DiscussionBoard.errors[2896070].message }}
  • Profile picture of the author weaveronline
    yes..just explode the date input value and implode it in req format [ie yyyy-mm-dd] for mysql and insert
    Signature

    Thanks & Regards,
    Reach us at dukeduke600@gmail.com.
    Web Design| Logo Design | Banner Design | Web Development | Mobile Applications [iPhone/iPad/Android/Windows Phone]

    {{ DiscussionBoard.errors[2903704].message }}

Trending Topics