简体   繁体   中英

Mysql SELECT by date format as 24 hour

I am trying to select data from a table mysql but I need to format a varchar date column in a military format, but cannot get my desire output my query is below.

"SELECT STR_TO_DATE(`hour`, '%Y-%m-%d %k:%i') AS `time` FROM `dataTable`"

When I tried to echo the time below format appear.

2017-09-21 00:00:00

My desire output is below which is in 24 hour format and removing the seconds, any suggestions would be great.

2017-09-21 18:00

Use the Date_format function of mysql.

"SELECT DATE_FORMAT(`hour`, '%Y-%m-%d %H:%i') AS `time` FROM `dataTable`"

Explanation:


The STR_TO_DATE function always return the date in the format of 2008-09-15 22:23:00

SELECT STR_TO_DATE('Monday 15th September 2008 22:23:00', '%W %D %M %Y %H:%i:%s');
+----------------------------------------------------------------------------+
| STR_TO_DATE('Monday 15th September 2008 22:23:00', '%W %D %M %Y %H:%i:%s') |
+----------------------------------------------------------------------------+
| 2008-09-15 22:23:00                                                        | 
+----------------------------------------------------------------------------+
1 row in set (0.00 sec)

Check the above example: the second parameter in STR_TO_DATE function is the format of the parameter 1 to tell the function that in which format parameter 1 is passed in the function.

Reference taken from this link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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