[英]Format Date in php and store in mysql
我有此语句可以在mysql中保存日期,但我想将其保存为示例:
September 24, 2011, 6:39 am
。
还是我应该拥有它,而当我检索它时,我会像这样回显它?
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();
}
<?php
/*
* F --> for full month
* D -->day
* Y --> Year
* H -->Hour
* m -->minute
* A -- PM or AM
*/
$date = date("F d, Y, H:m A");
echo $date;
?>
输出将是
July 26, 2017, 21:07 PM
// 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
: 2011-09-24 06:39:00
: 2011-09-24 06:39:00
,MySQL会很高兴。
文件: strftime | 的strtotime
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.