2

Given a date as a string like 'October 12, 2010'?

Should there be some Date "type" I should be converting to. Or is it just a matter of converting it to another string formatted as '2010-10-12' If so, what is the simplest way to convert to the yyyy-mm-dd format given my starting format?

2 Answers 2

6

Yes, DATE fields in MySQL just need to be a string in the correct format. To convert your date to the right format, use date combined with strtotime.

$date = 'October 12, 2010';
$sqlDate = date('Y-m-d', strtotime($date)); // 2010-10-12

Note: 'Y-m-d' is for a DATE field, if you're using DATETIME, use 'Y-m-d H:i:s' instead.

Sign up to request clarification or add additional context in comments.

Comments

2
$date = new DateTime('October 12, 2010');
$sqldate = $date->format('Y-m-d');

DateTime is PHP 5 >= 5.2.0

You could also use strtotime() however that tends to break if you want to use a date in the far future (on 32b systems).

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.