简体   繁体   English

如何在PHP / MySql中将08:00:00更改为08:00

[英]How to change 08:00:00 to 08:00 in PHP/MySql

I have time type in mysql and I input time with format of HH:MM. 我在mysql中输入时间,并以HH:MM格式输入时间。 This is stored in DB as HH:MM:00. 它以HH:MM:00的形式存储在DB中。

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. 代替13:20:00,显示13:20等。

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. 我可以使用substr($ time,0,5),但是我想知道是否有更好的方法可以做到这一点。

Thanks in advance. 提前致谢。

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

I presume it's saved in a DB column of type TIME. 我认为它已保存在TIME类型的DB列中。 In that case, fix it in your query: 在这种情况下,请在查询中对其进行修复:

SELECT TIME_FORMAT(yourfield, '%H:%i') FROM table;

Shin, 胫,

considering you have a fixed length string in datbase, you are already using the best method to format your time (substr). 考虑到datbase中有固定长度的字符串,您已经在使用最佳方法来格式化时间(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: 但是,如果您担心代码的优美性,并且如果字符串不是固定长度(例如9:31:02而不是09:31:02),则可以使用以下代码:

$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.. 如果您想走那条路,这是php部分。

echo date("h:i"); 回声日期(“ h:i”);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM