从日期时间中剥离时间分量

我需要在不考虑时间成分的情况下,在 Mysql 中做一个日期比较,也就是说,我需要将“2008-11-0514:30:00”转换为“2008-11-05”

目前我正在做的是:

SELECT from_days(to_days(my_date))

有什么合适的方法吗?

115151 次浏览

Yes, use the date function:

SELECT date(my_date)

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.

date_col = CAST(NOW() AS DATE)

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

select date(somedate) is the most common.

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

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

You could use ToShortDateString();

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