Last Updated: February 25, 2016
·
618
· 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.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside