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

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

问题描述

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



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

 <?php 
//我的服务器有例如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);
?>

我的问题是,最好的解决方案是什么,保留服务器时区或使用用户时区? >
,如果我使用用户时区,我应该像我的例子那样保存时区名称?

解决方案

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

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

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?

解决方案

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天全站免登陆