Php 为什么DateTime::createFromFormat()方法没有定义?
下面是PHP代码Php 为什么DateTime::createFromFormat()方法没有定义?,php,Php,下面是PHP代码 public function storeUser($name, $email, $password, $str_birthday) { $uuid = uniqid('', true); $hash = $this->hashSSHA($password); $encrypted_password = $hash["encrypted_password"]; // encrypted password $salt = $hash["sal
public function storeUser($name, $email, $password, $str_birthday) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted_password"]; // encrypted password
$salt = $hash["salt"]; // salt
$dob = DateTime::createFromFormat('m/d/Y', $str_birthday)->format('Y-m-d');
$result = mysqli_query($this->db->connect(), "INSERT INTO users(unique_id, name, email, encrypted_password, birthday, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$dob', '$salt', NOW())");
// check for successful store
if ($result) {
// get user details
$result = mysqli_query($this->db->connect(), "SELECT * FROM users WHERE email = '$email'");
// return user details
return mysqli_fetch_array($result);
} else {
return false;
}
}
在浏览器上运行命令后,会显示以下部分未定义
$dob=DateTime::createFromFormat('m/d/Y',$str_birth)->格式('Y-m-d')代码>
但我已经找到了很多建议,可以使用该方法更改日期格式,将其存储到MySQL数据库中 实现这一点的简单方法是:
$dob = date("Y-m-d", strtotime($str_birthday));
您确定总是提供$str_birth
?注意文档中说,返回一个新的DateTime实例,或者失败时返回FALSE(因此在这些情况下不能调用->format('Y-m-d')
)。这里也有一条关于PHP版本的好评论,但消失了。请注意,PHP>=5.3.0可以使用DateTime::createFromFormat
。@jsoDateTime::createFromFormat
是可用的,但接下来的事情是->format()
函数似乎未定义。@例如,当我在Eclipse中开始键入DateTime::createFromFormat
时,建议这样做,但->format()
不可用,说“无默认建议”。这只是Eclipse中的问题还是在运行代码时的问题?请注意,->format()
并不总是可用,因为它取决于输入:DateTime::createFromFormat('m/d/Y','01/01/2015')
将返回DateTime
,但DateTime::createFromFormat('m/d/Y','01/01/xy')
将返回false
,因此显然您不能使用->-format()
那么,在没有解释的情况下推荐旧的、通用性较差的解决方案不是一个好主意。此外,你没有回答这个问题。