我无法将正确的时间记录到我的数据库中。现在我的计算机上显示的时间是 2014-1-25 10:38:30 但当我使用 $datetime=date('Y-m-d H:i:s', time()) 时记录时间记录为 2014-01- 25 16:34:02 在我的数据库中。我的数据库中的列类型是DATETIME。我已经将计算机的时区更改为UTC-05:00,因为这是我从中国带来的计算机。
我有一个发布按钮 这是 HTML
<span id="postbutton" style="cursor:pointer; font-weight:bold; font- size:20px;">Launch</span>
这是我的 JavaScript
$('#postbutton').click(function(){
var timestamp= new Date().getTime();
$.ajax({
url:'post_handle.php',
method:'POST',
data:{timestamp:timestamp},
success:function(data){
//do something;
}
})
这是我的 PHP:
if(isset($_POST['timestamp'])){
$timestamp=$_POST['timestamp'];
//here below is the time i want to record
$datetime=date('Y-m-d H:i:s',$timestamp);
$Insert=mysql_query("INSERT INTO `user_post` VALUES('','','$datetime') ")
最佳答案
首先,不要使用mysql_query。它不安全且已弃用,see here 。查找 PDO。
数据保存正常,格式正确,因此不是数据库问题。当您使用 PHP 设置时间时,您可以完全排除 DB。
您需要考虑 time() 的来源。这不是您的电脑时钟,否则分钟会相同,因此来源是其他的。尝试更改服务器(您使用 Apache 吗?)时区/本地化。
关于php - 无法将正确的时间记录到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21352828/