如何在 select 语句中格式化 MySQL 中的日期时间字段?
我有一列设置为日期时间,定义格式为 0000-00-00 00:00:00,现在我尝试使用 SELECT 语句进行查询,以在输出后格式化该日期。
我尝试了 DATE_FORMAT(date_field,'%m-%d-%Y')
,但这不起作用。我想取出月日年...
这可能吗,还是我必须在数据取出并且是 PHP 函数之后才执行此操作?
I have a column set to datetime and with a defined format of 0000-00-00 00:00:00, and now I am trying to query with a SELECT statement to format that date after coming out.
I tried DATE_FORMAT(date_field,'%m-%d-%Y')
, but that didn't work. I want to just pull out the month-day-year...
Is this possible, or do I have to do it after the data has been pulled out and is a PHP function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要为 DATE_FORMAT 函数指定别名,以便 PHP 可以识别返回字段。
如果没有别名,输出字段将为:
$DATE_FORMAT(date_field,'%m-%d-%Y')
使用别名:
$date_field
You would need to alias the DATE_FORMAT function so that the return field would be something that PHP can recognize.
Without an alias the outputted field would be:
$DATE_FORMAT(date_field,'%m-%d-%Y')
With an alias:
$date_field