php - 在 PHP/MySQL 中使用 TIMESTAMP 字段获取时区

标签 php mysql datetime timezone timestamp

我完全 100% 坚持这个问题,对于 PHP/MySQL 中的时区支持,没有任何答案对我来说 100% 有效,所以我在这里再次问这个问题,尽可能详细地使用代码示例.

以下是我目前遇到的问题,请阅读评论以了解我遇到了什么样的问题。基本上我可以在用户的​​时区中显示日期和时间,如果它是像这样的时间戳的形式“1262819848”但是 MySQL 的较新版本返回这样的时间戳而不是“2010-01-06 23:17:28” .

如果我可以将 "2010-01-06 23:17:28"传递给这个日期 ("m:d:y h:i:s A",$time_stamp) 那么一切都会很好,但它需要一个时间戳传入。我能做到这一点的唯一方法是在 MySQL 的 INTEGER 字段中存储一个真实的时间戳,或者获取值“2010-01-06 23:17:28”,然后将其转换为一个时间戳当我可以将已转换的数据存储在 INT 字段中时,我希望有很多开销。

<?PHP
/////////////////////////////////////////////////////////
//   Get a MySQL time / date and show in user timezone //
////////////////////////////////////////////////////////

//set the user's time zone for this page
//this assumes the user has already chosen a timezone and we are getting it from a PHP session value now
// example value is "America/New_York" which is -5
date_default_timezone_set($_SESSION['time_zone']);

// get a MySQL result with a time/date value in it
$sql = 'SELECT user_id,date from online_users WHERE user_id = 1';
$result = executeQuery($sql);

// set our date/time value from MySQL into a variable
if ($line_online = mysql_fetch_assoc($result)){
    $time_stamp = $line_online['date'];
}

// format our date/time value TIMESTAMP any way we like now!
// MySQL result MUST be in this format '1262291376'
$date_format = date("m:d:y h:i:s A",$time_stamp); // formats timestamp in mm:dd:yy 
echo $date_format;


/////////////////////////////////////////////////////////
//   Add a time / date to MySQL                       //
////////////////////////////////////////////////////////

// this add a time and date to MySQL and looks like this in the MySQL table "2010-01-06 23:17:28"
// that is a TIMESTAMP field in MySQL, newer version of mysql store a timestamp like this "2010-01-06 23:17:28" instead of like this "1262819848"
$sql = "INSERT INTO users (`id`, `datetime`) VALUES ('', UTC_TIMESTAMP())";
executeQuery($sql);

// So if I query this record and try to show it like I do in the code above in the "Get mysql result section" It will not work correctly because  
// because it is like this "2010-01-06 23:17:28" instead of like this "1262819848"
// The ONLY solution I have found is to store the UTC timestamp into an INTEGER field instead of a DATETIME or TIMESTAMP field.
// Everyone on this site says this is wrong and I could use the default date and time stuff but I have yet to get it working.
// If I could store a timestamp in mysql like a timestamp with no "-" dashes in it, then it would work correctly when I show it on page.
// Basicly I have this half done, I can show the time and date in a users timezone as long as I pass the PHP a real TIMESTAMP like this "1262819848"

?>

最佳答案

PHP 中的 strtotime() 是一种将数据库日期时间格式传递到 PHP 中的 UNIX 纪元时间戳的方法。它读取可理解的标准格式并转换为 UNIX 纪元时间戳,以后可以由 php 中的其他日期时间函数使用,例如 date()。

替代方案可以是这样的:

MySQL 中的 UNIX_TIMESTAMP() 函数可帮助您将特定列的日期时间或时间戳值转换为 UNIX 时间戳。如 Datetime To Unix timestamp 所述它可以快一点,因为这是在 MySQL 服务器端完成的。

这似乎更像是一个时间戳问题,而不是时区问题。

查看更多:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

http://www.php.net/strtotime

关于php - 在 PHP/MySQL 中使用 TIMESTAMP 字段获取时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2031645/

相关文章:

mysql - mysql 出现超出范围错误

php - 删除除一个表之外的所有表?

javascript - 保存到变量时循环获取上一个数字

python - 如何降低日期的精度(秒)

php - 在在线 C 编译器中显示错误

php - 尝试获取 JSON 数据,但没有任何反应

php - 在mysql表中插入html代码

python - Python 的 time.time() 时区是特定的吗?

php - PDO - 动态绑定(bind)参数 - 值为 0 奇怪的结果

php - 动态任务调度程序 - Laravel