PHP: remove seconds off of returned time value

If you only need to remove seconds (not PM needed), you can use both MYSQL or php:

MYSQL:

SELECT SUBSTRING(SEC_TO_TIME(seconds), 1, 5);

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


Use strtotime() -

echo date('g:ia', strtotime($timestamp));

The date() function - string date ( string $format [, int $timestamp = time() ] ) - where $timestamp is to be an integer Unix timestamp.


$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).


Have you tried strtotime()?

echo date( 'g:ia', strtotime("12:00:00") );

Tags:

Php