Last Updated: February 25, 2016
·
423
· shawnrice

Getting past date/timezone warnings for PHP in OS X

If you're scripting on a Mac with PHP and need to use date functions, you can't rely on the user to have set them in /etc/php.ini, so here's a neat little workaround.

<?php
if ( ! ini_get('date.timezone') ) {
  $tz = exec( 'tz=`ls -l /etc/localtime` && echo ${tz#*/zoneinfo/}' );
  ini_set( 'date.timezone', $tz );
}
?>

This should work well enough for OS X, but I don't think it works for many others.