gpt4 book ai didi

php - 在php中格式化日期并存储在mysql中

转载 作者:行者123 更新时间:2023-11-29 18:35:35 24 4
gpt4 key购买 nike

我有这个语句在 mysql 中保存日期,但我希望将其保存为示例:

2011 年 9 月 24 日上午 6:39

或者我应该拥有它,当我检索它时,我会像那样回显它吗?

try {
$date = date('Y-m-d H:i:s');
//insert into database with a prepared statement
$stmt = $db->prepare('INSERT INTO admin (username, password, email, register_date, active) VALUES (:username, :password, :email, :register_date, :active)');
$stmt->execute(array(
':username' => $_POST['username'],
':password' => $hashedpassword,
':email' => $_POST['email'],
':register_date' => $date,
':active' => $activasion
));
//else catch the exception and show the error.
} catch(PDOException $e) {
$error[] = $e->getMessage();
}

最佳答案

// original datetime string:
$str = 'September 24, 2011, 6:39 am';

$stmt->execute(array(
':username' => $_POST['username'],
':password' => $hashedpassword,
':email' => $_POST['email'],
':register_date' => strftime("%Y-%m-%d %H:%M:%S", strtotime($str)),
':active' => $activasion
));

日期将被格式化为:2011-09-24 06:39:00,MySQL 会很高兴。

Live demo

文档:strftime | strtotime

关于php - 在php中格式化日期并存储在mysql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45334837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com