3

我完全不知道插入到底是怎么回事。基本上,我试图做正确的事情,将数据库中的所有内容以 UTC 存储,然后根据用户的信息转换为本地时区。我有很多查询都可以正常工作,但是这个插入查询正在给工作带来麻烦。

$adt = date('Y-m-d') . " " . $_POST['Time'];
echo $adt;
$tmp_dt = new DateTime($adt, new DateTimeZone($tz));
echo $tmp_dt->format('H:i:s');  
$tmp_dt->setTimezone(new DateTimeZone('UTC'));
$UTC_time = $tmp_dt->format('H:i:s');
echo $UTC_time;

echo 'date_default_timezone_set: ' . date_default_timezone_get() . '<br />';
mysql_select_db($db_name, $JewelryDB);
mysql_query("SET time_zone = '+00:00';");

$updateSQL = sprintf("INSERT INTO reminder_cfg 
                    (id, day_of, day_of_advance, day_prior, default_time, week_prior, 2_week_prior, 3_week_prior, 4_week_prior, 5_week_prior, day_after, 2_week_after, 50_day_after) 
                    VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
                    ON DUPLICATE KEY UPDATE day_of = %s, day_of_advance = %s, day_prior = %s, default_time = %s, week_prior = %s, 2_week_prior = %s, 3_week_prior = %s, 4_week_prior = %s, 5_week_prior = %s, day_after = %s, 2_week_after = %s, 50_day_after = %s",
                   GetSQLValueString($jeweler, "text"),
                   GetSQLValueString($_POST['DayOf'], "text"),
                   GetSQLValueString($_POST['Advance'], "text"),
                   GetSQLValueString($_POST['DayPrior'], "text"),
                   GetSQLValueString($UTC_time, "text"),
                   GetSQLValueString($_POST['WeekPrior'], "text"),
                   GetSQLValueString($_POST['2WeeksPrior'], "text"),
                   GetSQLValueString($_POST['3WeeksPrior'], "text"),
                   GetSQLValueString($_POST['4WeeksPrior'], "text"),
                   GetSQLValueString($_POST['5WeeksPrior'], "text"),
                   GetSQLValueString($_POST['DayAfter'], "text"),
                   GetSQLValueString($_POST['2WeeksAfter'], "text"),
                   GetSQLValueString($_POST['50DaysAfter'], "text"),
                   GetSQLValueString($_POST['DayOf'], "text"),
                   GetSQLValueString($_POST['Advance'], "text"),
                   GetSQLValueString($_POST['DayPrior'], "text"),
                   GetSQLValueString($_POST['Time'], "text"),
                   GetSQLValueString($_POST['WeekPrior'], "text"),
                   GetSQLValueString($_POST['2WeeksPrior'], "text"),
                   GetSQLValueString($_POST['3WeeksPrior'], "text"),
                   GetSQLValueString($_POST['4WeeksPrior'], "text"),
                   GetSQLValueString($_POST['5WeeksPrior'], "text"),
                   GetSQLValueString($_POST['DayAfter'], "text"),
                   GetSQLValueString($_POST['2WeeksAfter'], "text"),
                   GetSQLValueString($_POST['50DaysAfter'], "text"));

$Result1 = mysql_query($updateSQL, $JewelryDB) or die("Insert query error: " . mysql_error());

代码中的 echo 语句显示我的时区转换按预期工作,但插入语句以某种方式减去 5 小时以在本地时区插入时间。这是一个共享/托管服务器,所以我不能简单地将系统时间设置为 UTC。我认为 MySQL“SET time_zone”会纠正这个问题。

请帮忙。这让我发疯了。

4

1 回答 1

0

Mysql makes a distinction between the timestamp data type and the confusingly similar datetime data type. The datetime type has an implicit time zone (defined by you according to the weather outside your window), while the values stored in the timestamp type are implicitly converted to unixtime (UTC). If you want your time zone conversions to stick, you should store your values as datetime rather than timestamp.

于 2013-04-04T10:50:35.013 回答