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;
}

Unix command to format a number of bytes as a human readable value

It took me a while, but I finally figured out how to print a number from a bash script properly formatted with commas as thousand’s separators. For those like me who weren’t in the know, the magical incantation is:

  printf "%'d" 123456

That will format 123456 as 123,456. Much easier to read when you’re dealing with large numbers such as the sizes of files in gigabytes.

So now if I could only find the Unix command that took a number of bytes and turned it into an approximate value with GB or MB suffixes.