0

I have a form which includes 3 date of birth inputs like the following:

  <label>Date of Birth</label>
  <input type='text' size='2' maxlength='2' name='DOB[2]' />
  <input type='text' size='2' maxlength='2' name='DOB[3]' />
  <input type='text' size='4' maxlength='4' name='DOB[1]' />

The order of the inputs work as month/day/year. I am sending this to my script which then implodes the DOB array like such (Thanks to @Matt H.):

if(isset($_userData['DOB']))
    $_userData['DOB'] = implode('-', $_userData['DOB']);

Now, the problem is, this implodes it to the improper format of month/day/year, which is not the order of the array I set, but the order of the inputs. Am I stuck with having to manually concatenate the array into the format I need for MySQL (year/month/day) ?

1
  • Yup, but on the other hand, more control there seems a good way to avoid obscure bugs. I'd go for explicitly setting the order any day. Commented Jul 21, 2011 at 16:17

2 Answers 2

2

Sort the array first:

if(isset($_userData['DOB'])) {
    ksort($_userData['DOB']);
    $_userData['DOB'] = implode('-', $_userData['DOB']);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I get an error that invalid arguments are passed in the implode function. Do I have to do this outside of it first?
Yes, you do, sorry, ksort returns true/false. I've edited my answer.
1

Try:

if(isset($_userData['DOB'])){
    ksort($_userData['DOB']);
    $_userData['DOB'] = implode('-', $_userData['DOB']);
}

This will sort the array $_userData['DOB'] by key. Based on your input elements, it is going to become:

$_userData['DOB'] = array(
    1 => 'YYYY',
    2 => 'MM',
    3 => 'DD'
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.