简体   繁体   English

如何使用TIME_FORMAT从SQL数据库将HH:MM:SS转换为H:SS AM / PM

[英]how to convert HH:MM:SS to H:SS AM/PM from SQL database with TIME_FORMAT

This is the working version of the query. 这是查询的工作版本。 I only needed to save the new value with AS.Thank You to Andy. 我只需要用AS保存新值。谢谢您Andy。

$Wednesday = mysqli_query($conn, "SELECT *,TIME_FORMAT(class_start,'%l:%i %p') AS new_class_start,TIME_FORMAT(class_end,'%l:%i %p') AS new_class_end
FROM wednesday INNER JOIN classes_new ON wednesday.classes_id=classes_new.classes_id WHERE classes_status='0' AND class_start NOT LIKE ''ORDER BY class_start;");

I have a table with two datatype TIME columns when I run the query below the time value is returned as hh:mm:ss . 在运行下面的查询时,我有一个包含两个数据类型TIME列的表,时间值返回为hh:mm:ss。 I would like to convert it to h:mm AMPM. 我想将其转换为h:mm AMPM。 I have tried using javascript but I'm sure this is possible in MySQL. 我尝试使用javascript,但我确信这在MySQL中是可能的。 I have attempted to use TIME_FORMAT but I think I am getting the wrong syntax. 我尝试使用TIME_FORMAT,但是我认为语法错误。 After the below query is run the values are echoed in a while loop. 运行以下查询后,将在while循环中回显值。 Please Advise on the best way to convert the time values. 请提供有关转换时间值的最佳方法的建议。 Thanks! 谢谢!

$Wednesday = mysqli_query($conn, "SELECT *,TIME_FORMAT(wednesday.class_start,'%h:%i %p')

FROM wednesday INNER JOIN classes_new ON wednesday.classes_id=classes_new.classes_id WHERE classes_status='0' AND class_start NOT LIKE ''ORDER BY class_start;"); 从星期三INNER JOIN classes_new开到wednesday.classes_id = classes_new.classes_id WHERE classes_status ='0'AND class_start不喜欢“ ORDER BY class_start;”);

This should work 这应该工作

TIME_FORMAT(your_time,"%h:%i %p") AS your_time_formatted;

EDIT! 编辑! Added AS 添加了AS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM