MySQL中的UNIX_TIMESTAMP和NOW()的区别 [英] Difference between UNIX_TIMESTAMP and NOW() in MySQL

查看:102
本文介绍了MySQL中的UNIX_TIMESTAMP和NOW()的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个博客,用户可以评论。我使用 NOW()插入他们发表评论的时间,然后使用 date('j M Y',存储时间戳)显示他们发布的时间。



我想知道NOW()返回最终用户的 locatime或我服务器上的本地时间
更适合使用 UNIX_TIMESTAMP NOW()计算用户发布的本地时间

解决方案

函数 NOW()时间字符串,由您的MySQL服务器的时区确定。



但是,使用 UNIX_TIMESTAMP(),以GMT格式表示。这样做使得根据访问者的国家(例如使用JavaScript)更容易格式化。



如果您仍然想使用 DATETIME 列,您可以使用 UTC_TIMESTAMP()存储时间(它格式化日期,如 NOW()但表示在UTC);它应该在所有其他方面或多或少地相同。


I have a blog where users can comment. I insert the time at which they posted a comment using NOW() and then use date('j M Y', stored timestamp) to show the time at which they posted.

I want to know does NOW() return the locatime of the end user or the localtime at my server. Is it better suited to use UNIX_TIMESTAMP than NOW() to calculate the localtime at which users posted a comment.

解决方案

The function NOW() generates a formatted date-time string, determined by the time zone of your MySQL server.

However, it would be better to store times using UNIX_TIMESTAMP(), which is expressed in GMT. Doing so makes it easier to format it according to the country of a visitor (e.g. using JavaScript).

If you still want to use DATETIME columns, you can store times using UTC_TIMESTAMP() (it formats a date like NOW() but expresses it in UTC); it should more or less work the same in all other aspects.

这篇关于MySQL中的UNIX_TIMESTAMP和NOW()的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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