dongwen7730
2015-01-12 06:00 阅读 40
已采纳

将字符串传递给sprintf语法

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条回答 默认 最新

  • 已采纳
    duanran8648 duanran8648 2015-01-12 06:05

    Try with -

    $dobexpanded =sprintf("%04d-%02d-%02d", $year, $month, $day);
    $dateofbirth = date('Y-m-d', strtotime($dobexpanded));
    var_dump($dobexpanded);
    var_dump($dateofbirth);
    
    点赞 评论 复制链接分享
  • dougong5817 dougong5817 2015-01-12 06:04

    try this:

    date('Y-m-d', mktime(0, 0, 0, $month, $day, $year));
    
    点赞 评论 复制链接分享

相关推荐