如何将当前时间戳插入数据库?

时间:2018-08-15 18:49:01

标签: php mysql

我一直在尝试查询和研究,但是我不明白为什么它不起作用。

IP记录(VARCHAR)保存在数据库表中,但是插入记录时我无法保存记录DATETIME

$sql = "INSERT INTO ips (ip, fecha) VALUES (:ip, :CURRENT_TIMESTAMP)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
    ':ip' => htmlentities($_POST['ip']),
    ':CURRENT_TIMESTAMP' => htmlentities(CURRENT_TIMESTAMP)
));

1 个答案:

答案 0 :(得分:0)

使用PHP的时间功能:

$sql = "INSERT INTO ips (ip, fecha) VALUES (:ip, :CURRENT_TIMESTAMP)";
$t = time();
$today = date("Y-m-d", $t);
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
    ':ip' => htmlentities($_POST['ip']),
    ':CURRENT_TIMESTAMP' => $today