开发者

How to change 08:00:00 to 08:00 in PHP/MySql

开发者 https://www.devze.com 2023-01-18 06:22 出处:网络
I have time type in mysql and I input time with format of HH:MM. This is stored 开发者_开发问答in DB as HH:MM:00.

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.


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;


SELECT DATE_FORMAT(timefield,'%H:%i');


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


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

echo date("h:i");

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

0

精彩评论

暂无评论...
验证码 换一张
取 消