为什么DateTime :: createFromFormat()方法结果未定义?

时间:2015-11-15 13:46:55

标签: 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["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_birthday)->format('Y-m-d');

但我已经发现很多建议使用该方法来更改日期格式以将其存储到MySQL数据库中。

1 个答案:

答案 0 :(得分:-2)

实现这一目标的更简单方法:

$dob = date("Y-m-d", strtotime($str_birthday));