哪种方法在MySQL中存储日期和时间更好? [英] Which is the better way of storing date and time in MySQL?

查看:161
本文介绍了哪种方法在MySQL中存储日期和时间更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以以下格式将日期和时间存储在数据库中

i would like to store the date and time in the following format in the database

a)DD-MM-YYYY或我可以加起来HH-MM b)由于我的服务器位于美国,所以我想根据IST来获取时间.现在将是格林尼治标准时间:+5:30

a) DD-MM-YYYY or i could add up HH-MM b) as my servers are located in US i would like to fetch the time as per IST. now that will be GMT : +5:30

我以前曾以timestamp(int(11)):1291746600的格式将日期存储在mysql中.我曾经用strtotime()转换日期; 目前,我存储日期的目的只是为了存储和显示.将来我想算一下的天数,月数等.

earlier i used to store the date in mysql in this format timestamp(int(11)):1291746600. i used to convert the date with strtotime(); currently my intention of storing the date is just to store and display. in the future i would like to calculate the no. of days, months etc.

这将是对此的最佳解决方案?

which would be the best possible solution for this?

P:S:如果有人可以向我解释要使用哪种数据类型以及如何在PHP中使用它,我将不胜感激.

P:S : i would appreciate if someone could explain me which datatype to use and how to use it with PHP.

推荐答案

使用日期函数.

您需要使用任何特定格式输出字段,可以使用

Any specific format you need to output the fields in, you can create from a DATETIME field using DATE_FORMAT().

MySQL不支持DATETIME字段中的时区-您通常会设置服务器上的全局时区并使用它.

MySQL doesn't support time zones in DATETIME fields - you will usually set a global time zone on the server and use that.

在以下问题中,有关时区的相关阅读很好:

There's good related reading on Timezones in these questions:

  • MySQL: keep server timezone or user timezone?
  • Lost in dates and timezones

这篇关于哪种方法在MySQL中存储日期和时间更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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