I need to store both time and date in the mysql. So I used of NOW()
function for that. But I don't know what should I use for type column im phpmyadmin. It should be noted that NOW()
returns both time and date like this:
我需要在mysql中存储时间和日期。所以我使用了NOW()函数。但我不知道我应该使用什么类型列im phpmyadmin。应该注意的是,NOW()返回时间和日期,如下所示:
2014-11-11 12:45:34
Here is a solution, I can use of a separator for separating date and time (2014-11-11
and 12:45:34
) and then store them in the DATE type and TIME type individually. Or I can use of VARCHAR type for storing both of them in one column. But I think these ways are not standard. what is standard type for storing both date and time ?
这是一个解决方案,我可以使用分隔符来分隔日期和时间(2014-11-11和12:45:34),然后分别将它们存储在DATE类型和TIME类型中。或者我可以使用VARCHAR类型将它们存储在一列中。但我认为这些方式并不标准。什么是存储日期和时间的标准类型?
Here is my query: (also I don't know why NOW()
function does not works)
这是我的查询:(我也不知道为什么NOW()函数不起作用)
INSERT INTO table (timedate) VALUES (NOW())
2 个解决方案
#1
30
DATE: It is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD'
format. The supported range is '1000-01-01' to '9999-12-31'.
日期:它用于具有日期部分但没有时间部分的值。 MySQL以'YYYY-MM-DD'格式检索并显示DATE值。支持的范围是“1000-01-01”到“9999-12-31”。
DATETIME: It is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS'
format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
DATETIME:它用于包含日期和时间部分的值。 MySQL以'YYYY-MM-DD HH:MM:SS'格式检索并显示DATETIME值。支持的范围是'1000-01-01 00:00:00'到'9999-12-31 23:59:59'。
TIMESTAMP: It is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
TIMESTAMP:它用于包含日期和时间部分的值。 TIMESTAMP的范围为'1970-01-01 00:00:01'UTC到'2038-01-19 03:14:07'UTC。
TIME: Its values in 'HH:MM:SS' format
(or 'HHH:MM:SS' format for large hours values). TIME values may range from '-838:59:59' to '838:59:59'
. The hours part may be so large because the TIME type can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).
TIME:其值为“HH:MM:SS”格式(或“HHH:MM:SS”格式,表示大小时值)。 TIME值的范围可以从'-838:59:59'到'838:59:59'。小时部分可能很大,因为TIME类型不仅可用于表示一天中的时间(必须小于24小时),还可用于表示两个事件之间的经过时间或时间间隔(可能远大于24小时,甚至是负面的)。
#2
3
Saty described the differences between them. For your practice, you can use datetime
in order to keep the output of NOW()
.
萨蒂描述了他们之间的差异。对于您的练习,您可以使用日期时间来保持NOW()的输出。
For example:
例如:
CREATE TABLE Orders
(
OrderId int NOT NULL,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (OrderId)
)
You can read more at w3schools.
您可以在w3schools阅读更多内容。
#1
30
DATE: It is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD'
format. The supported range is '1000-01-01' to '9999-12-31'.
日期:它用于具有日期部分但没有时间部分的值。 MySQL以'YYYY-MM-DD'格式检索并显示DATE值。支持的范围是“1000-01-01”到“9999-12-31”。
DATETIME: It is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS'
format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
DATETIME:它用于包含日期和时间部分的值。 MySQL以'YYYY-MM-DD HH:MM:SS'格式检索并显示DATETIME值。支持的范围是'1000-01-01 00:00:00'到'9999-12-31 23:59:59'。
TIMESTAMP: It is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
TIMESTAMP:它用于包含日期和时间部分的值。 TIMESTAMP的范围为'1970-01-01 00:00:01'UTC到'2038-01-19 03:14:07'UTC。
TIME: Its values in 'HH:MM:SS' format
(or 'HHH:MM:SS' format for large hours values). TIME values may range from '-838:59:59' to '838:59:59'
. The hours part may be so large because the TIME type can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).
TIME:其值为“HH:MM:SS”格式(或“HHH:MM:SS”格式,表示大小时值)。 TIME值的范围可以从'-838:59:59'到'838:59:59'。小时部分可能很大,因为TIME类型不仅可用于表示一天中的时间(必须小于24小时),还可用于表示两个事件之间的经过时间或时间间隔(可能远大于24小时,甚至是负面的)。
#2
3
Saty described the differences between them. For your practice, you can use datetime
in order to keep the output of NOW()
.
萨蒂描述了他们之间的差异。对于您的练习,您可以使用日期时间来保持NOW()的输出。
For example:
例如:
CREATE TABLE Orders
(
OrderId int NOT NULL,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (OrderId)
)
You can read more at w3schools.
您可以在w3schools阅读更多内容。