如何在 select 语句中格式化 MySQL 中的日期时间字段?

发布于 2024-10-08 08:25:47 字数 201 浏览 0 评论 0原文

我有一列设置为日期时间,定义格式为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终遇你 2024-10-15 08:25:47

您需要为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文