LoginSignup
3
2

More than 5 years have passed since last update.

【php】DBの年/月/日をinput type="date"に初期値を渡したいとき

Posted at

input type="date" のvalueには 2018-1-3 ではなく、2018-01-03のような形式で渡さないといけない。

方法

  • sprintf()を使って数値の桁数をそろえる
  • 結合する
$year = $data->year;  //2018
$month = $data->month;  //1
$day = $data->day;  //3

//1桁の場合は0をつけて桁数をそろえる  
$month = sprintf('%02d', $month);  //01
$day = sprintf('%02d', $day);  //03

//結合
return $today = $year.'-'.$month.'-'.$day;  //2018-01-03
3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2