0

Possible Duplicate:
convert php date to mysql format

I have a date field which lets users type in their birthday in m/d/Y format (for example 06/01/1982) but just in case I also made my validation accept month and day values without any leading 0 (6/1/1982).

How can I convert these dates to Y-m-d for use in MySQL?

Other valid dates are 6/01/1982 and 06/1/1982.

2

2 Answers 2

0

You could use date() and strtotime() together like such:

$date = date('Y-m-d', strtotime('06/01/1982'));

That will give you

1982-06-01
Sign up to request clarification or add additional context in comments.

Comments

0

I would validate all input using strtotime (which will handle all major date formats).

On the insert, cast it to the proper format using the inverse: strftime:

$userValidatedDate = strtotime($_GET['date']);
//...
$sqlDate = strftime('%Y-%m-%d',$userValidatedDate);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.