我只是想把日期作为DATE(XXXX-XX-XX格式)传递给sql,但我似乎无法让sprintf接受我的字符串日期组件。我似乎正确地遵循文档。使用php5.5
$year = ($_POST['year']);
$month = ($_POST['month']);
$day = ($_POST['day']);
var_dump显示:
string(4) "1991" string(1) "8" string(1) "5"
然而这两个var_dump都是空的:
$dobexpanded =sprintf("%04s-%02s-%02s", $year, $month, $day);
$dateofbirth = (STR_TO_DATE('$dobexpanded', '%Y-%m-%d'));
var_dump($dobexpanded);
var_dump($dateofbirth);
答案 0 :(得分:1)
试试这个:
date('Y-m-d', mktime(0, 0, 0, $month, $day, $year));
答案 1 :(得分:1)
尝试 -
$dobexpanded =sprintf("%04d-%02d-%02d", $year, $month, $day);
$dateofbirth = date('Y-m-d', strtotime($dobexpanded));
var_dump($dobexpanded);
var_dump($dateofbirth);