PHP:从返回的时间值中删除秒数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14392001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: remove seconds off of returned time value
提问by Plummer
I need to remove the seconds off of a returned time value. I get
我需要从返回的时间值中删除秒数。我得到
12:00:00
I want
我想要
12:00pm
I tried using date()but it kept returning the time as 1:00am for everything.
我尝试使用,date()但它一直将所有时间返回为凌晨 1:00。
echo date('g:ia', $timestamp);
回答by Sean
Use strtotime()-
使用strtotime()-
echo date('g:ia', strtotime($timestamp));
The date()function - string date ( string $format [, int $timestamp = time() ] )- where $timestampis to be an integer Unix timestamp.
的date()功能- string date ( string $format [, int $timestamp = time() ] )-其中,$timestamp是为整数Unix时间戳。
回答by JvO
$timestamp = $timestamp - ($timestamp % 60);
In other words, use the modulo function to substract the number of seconds in the current minute from the time.
换句话说,使用模函数从时间中减去当前分钟的秒数。
Somtimes, simpler is better (and a lot more efficient than strtotime).
有时,越简单越好(而且比 strtotime 效率更高)。
回答by Luca C.
If you only need to remove seconds (not PM needed), you can use both MYSQL or php:
如果您只需要删除秒(不需要 PM),则可以同时使用 MYSQL 或 php:
MYSQL:
MYSQL:
SELECT SUBSTRING(SEC_TO_TIME(seconds), 1, 5);
PHP:
PHP:
$cleantime=substr($time,0,-3);
both will strip off seconds and keep hours and minutes
两者都将去除秒并保留小时和分钟
thanks to Jahanzeb for enhancing the PHP version to support also > 99 hours
感谢 Jahanzeb 增强 PHP 版本以支持 > 99 小时
回答by Kermit
Have you tried strtotime()?
你试过strtotime()吗?
echo date( 'g:ia', strtotime("12:00:00") );

