MySQL:保持服务器时区或用户时区? [英] MySQL: keep server timezone or user timezone?

查看:37
本文介绍了MySQL:保持服务器时区或用户时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑在我的网站中存储用户登录的日期,但我不知道什么是最合乎逻辑的解决方案.

I'm thinking about to store dates on user login in my site, but I don't know what is the most logical solution.

最初,我想使用服务器时区,然后使用服务器机器日期和用户机器日期之间的差异操作来管理它,但我也考虑过使用时区和 php 类日期直接更改它,所以:

Initially, I though to use server timezone, and then to manage it using difference operations between server machine date and user machine date, but I've also considered to change it direclty using timezone and php class date, so:

<?php
// my server has for example America/New_York timezone
$user_timezone = "Europe/Rome";
date_default_timezone_set ($user_timezone);
$date = date ('Y-m-d H:i:s');
$sql = "UPDATE users SET user_last_modify = '$date', user_timezone = '$user_timezone' WHERE user_id = 'some id' LIMIT 1;";
$res = mysql_query ($sql);
?>

我的问题是,最好的解决方案是保留服务器时区还是使用用户时区?
如果我使用用户时区,我是否也应该像我的示例一样保存时区名称?

My ask is, what is the best solution, keep server timezone or use user timezone?
and if I use user timezone, should I save the timezone name too as in my example?

推荐答案

我建议使用服务器时区或 UTC,而不是为每个用户存储不同的数据.这样,您的数据库将完全一致,并且您可以执行一些操作,例如比较,而不必为每个条目获取 user_timezone 列并执行转换(这不是完全免费的)

I'd recommend using server timezone or UTC, rather than storing different data for each user. This way, your database will be fully consistent, and you can perform some operations like comparisons without having to, for each entry, fetch the user_timezone column and perform the conversion (which is not fully free)

这篇关于MySQL:保持服务器时区或用户时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆