php 如何在 MySQL 中从 DATETIME 中分离 DATE 和 TIME
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12337195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to part DATE and TIME from DATETIME in MySQL
提问by Miss Rosy
I am storing a DATETIMEfield in a Table. After Storing the value its come like 2012-09-09 06:57:12.
我将DATETIME字段存储 在表中。存储值后,它就像 2012-09-09 06:57:12 一样。
For storing I am using the syntax:
为了存储我使用的语法:
date("Y-m-d H:i:s");
Now My Question is, While Fetching the data, How can get both date and time separate, using the single MySQL query.
现在我的问题是,在获取数据时,如何使用单个 MySQL 查询将日期和时间分开。
Date like 2012-09-09and time like 06:57:12.
日期如2012-09-09和时间如06:57:12。
回答by John Woo
You can achieve that using DATE_FORMAT()(click the link for more other formats)
您可以使用DATE_FORMAT()来实现(单击链接了解更多其他格式)
SELECT DATE_FORMAT(colName, '%Y-%m-%d') DATEONLY,
DATE_FORMAT(colName,'%H:%i:%s') TIMEONLY
SQLFiddle Demo
SQLFiddle 演示
回答by Frank Thomas
per the mysql documentation, the DATE() function will pull the date part of a datetime feild, and TIME() for the time portion. so I would try:
根据 mysql 文档, DATE() 函数将提取日期时间字段的日期部分,以及时间部分的 TIME() 。所以我会尝试:
select DATE(dateTimeFeild) as Date, TIME(dateTimeFeild) as Time, col2, col3, FROM Table1 ...
回答by Sudhir Bastakoti
Try:
尝试:
SELECT DATE(`date_time_field`) AS date_part, TIME(`date_time_field`) AS time_part FROM `your_table`
回答by Thushara Buddhika
Simply,
SELECT TIME(column_name), DATE(column_name)
简单地说,
SELECT TIME(column_name), DATE(column_name)
回答by Swapnil
For only date usedate("Y-m-d");
仅限日期使用date("Y-m-d");
and for only time usedate("H:i:s");
并且仅供时间使用date("H:i:s");

