2

我有一个 MySQL 查询,它在数据库中保存评论并保存时间戳我想将时间戳保存在另一个时区设置中我可以通过以下查询来做到这一点:

INSERT INTO tahminler 
(comment, comment_text, match_id, user_id, timestamp) 
VALUES ('$comment','$comment_text', $id, $user_id, now())
4

3 回答 3

0

是的。你可以date_default_timezone_set()在 PHP 上做到这一点。

<?php
echo date('Y-m-d H:i:s');// Your local server time
date_default_timezone_set('America/Los_Angeles');
echo "<br>";
echo date('Y-m-d H:i:s');// Los Angeles time

now()因此,在设置您选择的时区后替换您date('Y-m-d H:i:s');的。

于 2013-09-26T08:07:02.277 回答
0

你可以试试这个

//default timezone
$date = new DateTime(null);
echo 'Default timezone: '.$date->getTimestamp().'<br />'."\r\n";

//America/New_York
$date = new DateTime(null, new DateTimeZone('America/New_York'));
echo 'America/New_York: '.$date->getTimestamp().'<br />'."\r\n";

获取常规时间戳并添加 UTC 偏移量

  //America/New_York
    $date = new DateTime(null, new DateTimeZone('America/New_York'));
    echo 'America/New_York: '.($date->getTimestamp() + $date->getOffset()).'<br />'."\r\n";
于 2013-09-26T08:08:43.523 回答
0

您可以像这样添加列时间戳来更改您的表....

 ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP 
 ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP 

这会添加一个名为“lastUpdated”的列,其默认值是当前日期/时间。当该记录更新时(假设 5 分钟后),时间戳将自动更新为当前时间。

于 2013-09-26T08:14:43.597 回答