gpt4 book ai didi

php - 根据特定时区将当前时间插入mysql数据库

转载 作者:行者123 更新时间:2023-11-29 08:28:20 25 4
gpt4 key购买 nike

在我的 MySql 数据库中,post_time 字段的值已设置为 NOT NULL DEFAULT CURRENT_TIMESTAMP

我使用以下 MySql insert 语法和 html 表单来收集访问者的数据,当访问者提交表单时,通过隐藏字段将帖子的日期和时间插入到 MySql 数据库中。

$insertSQL = sprintf("INSERT INTO entry (post, post_time) VALUES (%s, %s)",                     
GetSQLValueString($_POST['post'], "text"),
GetSQLValueString($_POST['post_time'], "date"));
<小时/>
  <form action="<?php echo $editFormAction;?>" method="post" name="form1" id="form1">
<table width="50%" align="center">
<tr valign="baseline">
<td align="right" nowrap="nowrap">Post:</td>
<td>
<textarea name="post" cols="50" rows="5"></textarea>
</td>
</tr>
<tr valign="baseline">
<td align="right" nowrap="nowrap">
<input type="hidden" name="post_time" value="<?php echo $_POST['TIMESTAMP DEFAULT CURRENT_TIMESTAMP']; ?>" />
<input type="hidden" name="MM_insert" value="form1" />
</td>
<td>
<input type="submit" name="submit_post" value="Submit Post" />
</td>
</tr>
</table>
</form>

如何根据共享服务器上的预定义时区收集有关日期和时间的数据(该服务器具有基于 GMT 的时区)?

那么我该如何执行以下sql语法呢?

已编辑:

SET time_zone = '美国/纽约';

谢谢

最佳答案

post_time设置为默认值CURRENT_TIMESTAMP,MySQL将自动更新该列。除非您想覆盖它,否则不需要设置它。

设置 date_default_timezone_set() 对于 MySQL 来说并不重要 - 如果 MySQL 服务器设置了自己的日期,则需要为 MySQL 服务器设置时区。

最后,您不应将 post_time 隐藏字段放入表单中 - 您的服务器应该 (A) 生成此值,或 (B) 让 MySQL 自动生成此值。

关于php - 根据特定时区将当前时间插入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17347386/

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