KaoriYui
KaoriYui

Reputation: 912

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

Upvotes: 1

Views: 2076

Answers (1)

Code Lღver
Code Lღver

Reputation: 15603

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.

Upvotes: 4

Related Questions