Piping PHP CLI output to less broken

Today I had an interesting problem.

When I ran:

 # php test.php | less

The ‘less’ program entered input mode and the up and down arrow (and j, k keys) weren’t working.

Eventually I figured out a solution:

 # php test.php < /dev/null | less

The PHP CLI seems to be interfering with the terminal and piping in /dev/null to PHP CLI stdin fixes the problem!

PHP DateTimeZone->getOffset()

Today I had to use PHP’s DateTimeZone->getOffset() to get a timezone offset. It needed formatting which I did with:

function timezone_offset() {
  $offset = timezone()->getOffset( now() );
  $hours = round( abs( $offset ) / 3600 );
  $minutes = round( ( abs( $offset ) - $hours * 3600 ) / 60 );
  $result = ( $offset < 0 ? '-' : '+' )
    . ( $hours < 10 ? '0' : '' ) . $hours
    . ':'
    . ( $minutes < 10 ? '0' : '' ) . $minutes; 
  return $result;
}