php 将 mysql TIME 从 24 小时制转换为 AM/PM 格式

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

Converting mysql TIME from 24 HR to AM/PM format

phpmysqldatetimedatetime

提问by Ali

I want to display the TIME field from my mysql table on my website, but rather than showing 21:00:00 etc I want to show 8:00 PM. I need a function/code to do this or even any pointers in the right direction. Will mark the first reply with some code as the correct reply.

我想在我的网站上显示我的 mysql 表中的 TIME 字段,但不是显示 21:00:00 等,我想显示 8:00 PM。我需要一个函数/代码来执行此操作,甚至需要任何指向正确方向的指针。将使用一些代码将第一个回复标记为正确回复。

采纳答案by Steve Klabnik

Check this out: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

看看这个:http: //dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

I'd imagine you'd want date_format().

我想你会想要 date_format()。

Example: DATE_FORMAT($date, "%r")

示例:DATE_FORMAT($date, "%r")

回答by JDGuide

Show the date & time data in AM/PM format with the following example...

使用以下示例以 AM/PM 格式显示日期和时间数据...

SELECT DATE_FORMAT(`t`.`date_field`,'%h:%i %p') AS `date_field` FROM `table_name` AS `t`

OR

或者

SELECT DATE_FORMAT(`t`.`date_field`,'%r') AS `date_field` FROM `table_name` AS `t`

Both are working properly.

两者都正常工作。

回答by phatduckk

You can also select the column as a unix timestamp using MYSQL's UNIX_TIMESTAMP()function. Then format it in PHP. IMO this is more flexible...

您还可以使用 MYSQL 的UNIX_TIMESTAMP()函数选择该列作为 unix 时间戳。然后用PHP格式化。海事组织这更灵活......

select a, b, c, UNIX_TIMESTAMP(instime) as unixtime;

The in PHP use the date()function & format it any way you want.

在 PHP 中使用该date()函数并以任何您想要的方式对其进行格式化。

<?php echo date('Y/m/d', $row->unixtime); ?>

The reason I like this method as opposed to formatting it in SQL is b/c, to me, the date's format is a display decision & (in my opinion) formatting the date in SQL feels wrong... why put display logic in your SQL?

我喜欢这种方法而不是在 SQL 中格式化它的原因是 b/c,对我来说,日期的格式是一个显示决定 &(在我看来)在 SQL 中格式化日期感觉是错误的......为什么要把显示逻辑放在你的SQL?

Now - if you're not processing the data in PHP and are doing adhoc queries then DATE_FORMAT()is the way to go. But if you're gonna have the data show up on the web I'd go with UNIX_TIMESTAMP()and do the formatting in PHP...

现在 - 如果您不是在 PHP 中处理数据并且正在执行临时查询,那么DATE_FORMAT()这就是要走的路。但是,如果您要将数据显示在网络上,我会使用UNIX_TIMESTAMP()PHP 进行格式化...

I mean... lets say you want to change how the date & time are displayed on the page... wouldn't it feel "off" to have to modify your SQL for a display tweak?

我的意思是...假设您想更改日期和时间在页面上的显示方式...是否必须修改您的 SQL 以进行显示调整会不会感觉“关闭”?

my 2 cents

我的 2 美分

回答by phatduckk

I had been trying to do the same and got this page returned from a Google search. I worked a solution for the time 21:00:00;

我一直在尝试做同样的事情,并从 Google 搜索中返回了此页面。我为 21:00:00 的时间工作了一个解决方案;

  • using DATE_FORMAT(<field>,'%l.%i%p')which returned 9.00PM

  • putting a LOWER()function around it to return 9.00pm

  • 使用DATE_FORMAT(<field>,'%l.%i%p')返回 9.00PM

  • LOWER()在它周围放置一个函数以返回 9.00pm

So the full code is; DATE_FORMAT(<field>,'%l.%i%p')

所以完整的代码是; DATE_FORMAT(<field>,'%l.%i%p')

Worked OK for me ...

对我来说工作正常......

回答by Martin York

Use DATE_FORMAT()

使用DATE_FORMAT()

DATE_FORMAT(<Fieled>,'%h:%i:%s %p')

DATE_FORMAT(<字段>,'%h:%i:%s %p')

or

或者

DATE_FORMAT(<Fieled>,'%r')

DATE_FORMAT(<字段>,'%r')