tags:

views:

41

answers:

4

I have time type in mysql and I input time with format of HH:MM. This is stored in DB as HH:MM:00.

Now when I display, it shows seconds as well.

I only want to show hour and minutes.

In stead of 13:20:00, showing 13:20 etc.

What is the best way to do it.

I can use substr($time, 0, 5), but I am wondering if there is a better way to do it.

Thanks in advance.

+1  A: 
SELECT DATE_FORMAT(timefield,'%H:%i');
Wrikken
Wrong parameter order, ad I think TIME_FORMAT is better suited.
Emil Vikström
Ah, fixed the order. And `time_format` is IMHO only useful when going over a 23 hour mark (i.e. you're displaying an _amount_ of time rather then a _point in time_).
Wrikken
+1  A: 

I presume it's saved in a DB column of type TIME. In that case, fix it in your query:

SELECT TIME_FORMAT(yourfield, '%H:%i') FROM table;
Emil Vikström
A: 

here is the php portion if you want to go that route..

echo date("h:i");

http://php.net/manual/en/function.date.php

RandyMorris
+1  A: 

Shin,

considering you have a fixed length string in datbase, you are already using the best method to format your time (substr). This gives you the least requirements for computing power.

If, however, you are worried about your code elegance and, if by any chance the string is not a fixed length (like 9:31:02 instead of 09:31:02 ) you can use this code:

$hours   = strtok( $dbTime, ':');
$minutes = strtok( ':');
$formattedTime = $hours . ':' . $minutes

i hope this helps Slavic

Slavic