【发布时间】:2013-08-27 22:45:41
【问题描述】:
我在使用简单的$dob 表单输入和使用 PDO 将数据库插入到数据类型为 date 的 mysql 表中时遇到了一些奇怪的问题。
输入表单(示例):
<select name="dob-month">
<option value="1">January</option>
</select>
<select name="dob-day">
<option value="01">1</option>
</select>
<select name="dob-year">
<option>2013</option>
</select>
PHP(基础):
$data = $_POST; //passed through mvc
$day = trim($data['dob-day']);
$month = trim($data['dob-month']);
$year = trim($data['dob-year']);
$birthdate = date('Y-m-d', strtotime($year."-".$month."-".$day));
$stmt = $this->core->dbh->prepare("INSERT INTO userinfo (birthdate) VALUES (:birthdate)");
$stmt->bindParam(':birthdate', $birthdate, PDO::PARAM_STR);
$stmt->execute();
但是它给出了没有日期的日期?所以只有 2013-12 工作正常,但这一天总是 -01?
和日期的选择格式有关吗?
【问题讨论】:
-
生日是什么字段类型?
-
$year、$month 和 $day 是什么,也许你需要 strtotime() ?
-
尝试使用
strtotime("$day $month $year")并注意语言环境对此的作用。你的问题让我想起了一点:strtotime With Different Languages? 和 how to php convert this time format?。