tags:

views:

50

answers:

3

Hello,

I have a mysql table field set as time type which stores data in the HH:mm:ss format. So when I list the data, it prints as, for example, 16:30:00. But I want to display hh:mm part only (not the seconds).

In case of datetime types, I can do date('H:i', '2010-03-16 16:30:00'). I mean I can retrieve any part. I wonder if there is any similar way like this for time only fields??

Please see, I can manipulate the time string to get rid of seconds in time part using str_replace, explode etc, I just wonder if there is any standard function there which I am not aware of.

Thanks.

+3  A: 

You can let MySQL return the data in the format you want using Date_Format()

edit: as fireeyedboy pointed out there's also a TIME_FORMAT() function.

VolkerK
DATE_FORMAT(date,format) .. it expects date as first parameter but i have got only time ???
It also accepts a TIME value. If you pass a TIME and try to access the DATE-part (e.g. %Y-%m-%d %H:%i) MySQL "assumes" 0000-00-00
VolkerK
You just need to specify your time in the format and not the date Ex: DATE_FORMAT(date,'HH:MM:SS')
Lex
There is also a TIME_FORMAT function.
fireeyedboy
BTW: from what I remember, I don't believe DATE_FORMAT exepts a TIME value.
fireeyedboy
I just tried it on a 5.0.67-community-nt server, and date\_format() accepts a TIME value. ...but why use it if there is also time\_format()? Good catch.
VolkerK
hello all .. thanks for infor. Time_Format looks great for what I need .. but I'll be using a php function strtotime. Thx.
+1  A: 

If you want to do this with PHP, you'd have to get a timestamp from the time first, e.g.

echo date('H:i', strtotime('16:30:00'));

will output 16:30. Strtotime will assume the time is for the current date then.

Gordon
Gordon - this does the trick.Thx.
+1  A: 

Another approach is:

SELECT UNIX_TIMESTAMP(time_field) AS tstamp FROM table;

Then you can use PHP's date() function to format it.

$time = date('H:i', $tstamp);
Greg K