将日期/时间从 PHP 保存到 SQL

发布于 2024-12-25 13:04:40 字数 345 浏览 0 评论 0原文

我想将 PHP 中的日期和时间保存到 SQL 中。下面是插入新记录的 SQL 语句(在类中的方法中找到):

INSERT INTO tbl_reviews (headline, text, date, rating, product_id, username)
            value (:headline, :text, :date, :rating, :product_id, :username)

在我的 .php 页面中,我使用 $_SERVER['REQUEST_TIME'] 调用当前日期和时间。但我仍然收到错误“日期时间值不正确”。我可以用什么来获取日期?

I want to save the date and time from PHP to SQL. Here is the SQL statement to insert new record (found in a method within a class):

INSERT INTO tbl_reviews (headline, text, date, rating, product_id, username)
            value (:headline, :text, :date, :rating, :product_id, :username)

And in my .php page, I call the current date and time using $_SERVER['REQUEST_TIME']. But still I'm getting the error "Incorrect datetime value". What can I use to get the date?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

妄司 2025-01-01 13:04:40

可以生成您的时间戳:

$timestamp = date('Y-m-d H:i:s');

这应该模仿 mysql 时间戳和日期时间格式。

假设 mysql 的时间戳与相关 php 服务器同步,您也可以只使用 mysql 当前时间戳函数:

NOW() 或 CURRENT_TIMESTAMP 或 CURRENT_TIMESTAMP()

Your timestamp can be generated:

$timestamp = date('Y-m-d H:i:s');

This should mimic the mysql timestamp and datetime formats.

Assuming that the mysql has its timestamp synchronized with the php server in question, you can also just use the mysql current timestamp functions:

NOW() or CURRENT_TIMESTAMP or CURRENT_TIMESTAMP()

夜唯美灬不弃 2025-01-01 13:04:40

是否必须是准确的请求时间?您可以让您的生活更轻松,只需使用:

INSERT INTO tbl_reviews (headline, text, date, rating, product_id, username)
            value (:headline, :text, now(), :rating, :product_id, :username)

一旦您的条目写入表中,MySQL 就会插入当前日期。

Does it have to be the exact request time? You could make your life easier and simply use:

INSERT INTO tbl_reviews (headline, text, date, rating, product_id, username)
            value (:headline, :text, now(), :rating, :product_id, :username)

MySQL inserts the current date as soon your entry is written to the table.

扛刀软妹 2025-01-01 13:04:40

这只是一个猜测,但 $_SERVER['REQUEST_TIME'] 返回一个浮点数,而不是有效的日期时间格式。

it is only a guess, but $_SERVER['REQUEST_TIME'] returns a float, not a valid datetime format.

睡美人的小仙女 2025-01-01 13:04:40

如果您需要当前时间,可以这样做:

INSERT INTO tbl_reviews (headline, text, date, rating, product_id, username)
INSERT INTO tbl_reviews ('$headline', '$text', CURDATE(), '$rating', '$product_id', '$username');

If you need the current time you can do it like that:

INSERT INTO tbl_reviews (headline, text, date, rating, product_id, username)
INSERT INTO tbl_reviews ('$headline', '$text', CURDATE(), '$rating', '$product_id', '$username');
风透绣罗衣 2025-01-01 13:04:40
date_default_timezone_set('US/Eastern');
$cur_date=date("Y-m-d");
date_default_timezone_set('US/Eastern');
$cur_date=date("Y-m-d");
动听の歌 2025-01-01 13:04:40

您可以使用 SQL 自己的 CURRENT_TIMESTAMP 值来获取自动格式化的时间戳。

至于$_SERVER['REQUEST_TIME'],您可以使用 time()microtime() 代替。

如前所述,REQUEST_TIME 以及 time() 和 microtime() 返回一个 UNIX 时间戳,其中基本上是自 UNIX 纪元以来经过的秒数,这与 DATETIME 字段期望的格式不同。

You can use SQL's own CURRENT_TIMESTAMP value to get an automatically formatted timestamp.

As for $_SERVER['REQUEST_TIME'], you can just use time() or microtime() instead.

As mentioned, REQUEST_TIME, as well as time() and microtime() return a UNIX timestamp, which is basically the amount of seconds that have passed since the UNIX epoch, which is not the same format as a DATETIME field expects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文