Omit seconds in Mysql time field

Asked

Viewed 844 times

4

Guys I have a field on my table of type time, where I save my time. Problem is he’s saving it like this: 10:30:01, how do I save him only the hour and minutes. That is without the seconds?

The field is like this in the comic book hora time DEFAULT NULL

  • What’s the problem? if you want to discard seconds use formatting(date_format())

  • how do I make it format the time without the seconds?

  • http://answall.com/a/74425/91

  • How you are inserting values into this field?

  • Saved in time standard, H:i:s

  • How are you saving your date today?

Show 1 more comment

2 answers

8

You can make time comparisons and presentations using the function date_format() Mysql to delete seconds. H represents the time in the 0-23 format and i minutes. Even if seconds are omitted from the Insert the field is saved with 00 seconds.

Example:

SELECT date_format('%H:%i', now())

List of arguments accepted by date_format

  • corrects the order of the parameters, DATE_FORMAT(date,format), in accordance with documentation

  • Okay, but I do the following select: select * from pedidos where I need to read all the fields of the requested table, and I have 2 time fields,hora_entrega and hora_pedido. With this formatting I apply?

  • @Marcelodeandrade was worth :D, corrected.

  • 1

    @Hugoborges needs to specify this formatting for these two fields, SELECT *, date_format(hora_entreta, '%H:%i') as hora_entrega, date_format(hora_pedido, '%H:%i') as hora_pedido FROM pedidos

6


Just as rray said, you have the following options DATE_FORMAT and TIME_FORMAT:

DATE_FORMAT(NOW(), "%H:%i");

TIME_FORMAT(NOW(), "%H:%i");

I believe that in your case, TIME_FORMAT be more appropriate:

SELECT *, TIME_FORMAT(hora_entrega, "%H:%i") as hora_entrega FROM pedidos
  • 1

    time_format seems more suitable :) +1

  • I understand, as I said in the comment, I have to apply this to a select where I read all the fields in the table. How to do this?

  • 2

    SELECT *, TIME_FORMAT(hora_entrega, "%H:%i") as hora_entrega FROM pedidos

Browser other questions tagged

You are not signed in. Login or sign up in order to post.