gpt4 book ai didi

php - 我的日期时间格式的日期被插入到我的表中,其中包含更多应该的零

转载 作者:行者123 更新时间:2023-11-29 00:13:10 25 4
gpt4 key购买 nike

我的数据库中有一个新闻表,其中包含“日期”字段,这是一个日期时间。

我正在向数据库中插入一个日期,我的日期格式为“2014 年 6 月 1 日,星期日”,我需要在插入之前将其转换为日期时间。

所以我将我的日期转换为日期时间:

$english = array(); //have words in english
$spanish = array(); //have words in spanish
$result_date = str_ireplace ($english , $spanish, $f['date']);
$date = DateTime::createFromFormat('l, j F, Y', $result_date);
$date = $data->format('Y-m-d H:i:s');

然后我插入:

$insert = $pdo->prepare("INSERT INTO news (title,date) VALUES (:title,:date)");  
$insert->bindValue(':title', $f['title']);
$insert->bindValue(':date', $date);
$insert->execute();

问题是,我在这样的数据库中获取日期:2014-05-28 02:57:46.000000

而且我不明白,为什么会出现最后 6 个零。

你知道它们为什么会出现吗?我该如何删除它??

最佳答案

datetimetimestamp 列支持精确到微秒,如 documentation 中所述:

A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision. In particular, any fractional part in a value inserted into a DATETIME or TIMESTAMP column is stored rather than discarded. With the fractional part included, the format for these values is 'YYYY-MM-DD HH:MM:SS[.fraction]', the range for DATETIME values is '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999', and the range for TIMESTAMP values is '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999'. The fractional part should always be separated from the rest of the time by a decimal point; no other fractional seconds delimiter is recognized. For information about fractional seconds support in MySQL, see Section 11.3.6, “Fractional Seconds in Time Values”.

您的值已正确存储,如果您想以特定方式检索它(例如没有小数秒),请使用 date_format() 将数据作为格式化字符串提取出来.在您的情况下,您似乎想要格式 '%y-%m-%d %h:%i:%s'

关于php - 我的日期时间格式的日期被插入到我的表中,其中包含更多应该的零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23975902/

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