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


Inserting current time into mysql database based on particular timezone

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

我使用以下MySql insert语法和html表单从访问者那里收集数据,其中date&当他们提交表单时,帖子的时间通过隐藏字段插入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服务器设置了自己的日期,则需要为其设置时区。

最后,您不应该在表单中隐藏post_time字段——您的服务器应该(A)生成这个值,或者(B)让MySQL自动生成这个值。

您不应该插入这样的值,而应该编写一个执行此操作的查询:

INSERT INTO entry (post, post_time) VALUES (?, UTC_TIMESTAMP())

输入的值是定义列的方式,而不是插入值的方式。