Mysql 从日期时间中剥离时间组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/267721/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:20:29  来源:igfitidea点击:

Mysql strip time component from datetime

sqlmysqldatetime

提问by abarax

I need to do a date comparison in Mysql without taking into account the time component i.e. i need to convert '2008-11-05 14:30:00' to '2008-11-05'

我需要在 Mysql 中进行日期比较而不考虑时间组件,即我需要将 '2008-11-05 14:30:00' 转换为 '2008-11-05'

Currently i am doing this:

目前我正在这样做:

SELECT from_days(to_days(my_date))

Is there a proper way of doing this?

有没有正确的方法来做到这一点?

回答by Robert Gamble

Yes, use the datefunction:

是的,使用日期函数:

SELECT date(my_date)

回答by abarax

select date(somedate)is the most common.

select date(somedate)是最常见的。

If you need to accommodate other formats, you can use:

如果需要容纳其他格式,可以使用:

SELECT DATE_FORMAT(your_date, '%Y-%m-%d');

回答by Andy Lester

In PostgreSQL you use the TRUNC() function, but I'm not seeing it for MySQL. From my brief Googling, it looks like you'll need to cast your DATETIME value to be just DATE.

在 PostgreSQL 中,您使用 TRUNC() 函数,但我没有在 MySQL 中看到它。从我的简短谷歌搜索来看,您似乎需要将 DATETIME 值转换为 DATE。

date_col = CAST(NOW() AS DATE)

See http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html

回答by Rajeev Kumar

Just a simple way of doing it date("d F Y",strtotime($row['date']))where $row['date']comes from your query

只是做一个简单的方法date("d F Y",strtotime($row['date'])),其中$row['date']来自查询

回答by alan naidon

You could use ToShortDateString();

你可以用 ToShortDateString();