PHP Date and Time

Unix Timestamp

The Unix Timestamp (Unix time, Unix epoch, or POSIX time) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT).

# Get the current timestamp in seconds.
$current_time = time();

# Get the current timestamp in microseconds.
$current_microtime = microtime(true);

Convert Seconds to Readable Time

function get_time_from_seconds( $seconds )
{
    $hours = floor($seconds / 3600);
    $mins = floor($seconds / 60 % 60);
    $secs = floor($seconds % 60);

    $hours = ( $hours > 0 ? ltrim($hours, '0') . ':' : '' );

    $mins = ( $mins > 0 ? $mins : '0' );
    $mins = ( $mins > 9 ? $mins : '0' . $mins );
    $mins = ( $hours > 0 ? $mins : ltrim($mins, '0') );
    $mins = ( $mins == '' ? '0:' : $mins . ':' );

    $secs = ( $secs > 0 ? $secs : '0' );
    $secs = ( $secs > 9 ? $secs : '0' . $secs );

    return $hours . $mins . $secs;
}

// Run some tests.
echo get_time_from_seconds( '10' ) . "<br>"; // 0:10
echo get_time_from_seconds( '65' ) . "<br>"; // 1:05
echo get_time_from_seconds( '400' ) . "<br>"; // 6:40
echo get_time_from_seconds( '800' ) . "<br>"; // 13:20
echo get_time_from_seconds( '3600' ) . "<br>"; // 1:00:00
echo get_time_from_seconds( '3606' ) . "<br>"; // 1:00:06
echo get_time_from_seconds( '3660' ) . "<br>"; // 1:01:00