0

I am just trying to pass a date into sql as a DATE (XXXX-XX-XX format), but I can't seem to get sprintf to accept my string date-components. I seem to be following the documentation correctly. using php5.5

$year = ($_POST['year']);
$month = ($_POST['month']); 
$day = ($_POST['day']);

var_dump shows:

 string(4) "1991" string(1) "8" string(1) "5"

however both of these var_dumps are empty:

$dobexpanded =sprintf("%04s-%02s-%02s", $year, $month, $day);
$dateofbirth = (STR_TO_DATE('$dobexpanded', '%Y-%m-%d'));
var_dump($dobexpanded);
var_dump($dateofbirth);

2 Answers 2

1

Try with -

$dobexpanded =sprintf("%04d-%02d-%02d", $year, $month, $day);
$dateofbirth = date('Y-m-d', strtotime($dobexpanded));
var_dump($dobexpanded);
var_dump($dateofbirth);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much. Kept it as strings, but the date format converted.
1

try this:

date('Y-m-d', mktime(0, 0, 0, $month, $day, $year));

1 Comment

thanks. I ended up using the answer above but I am confident this would have worked as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.