Jump to content

PHP: Remember to set time zone before use of strftime()


Recommended Posts

So, this is just a quick tip: I you use strftime() to create a date/time string then remember to set the default time zone first, or PHP will throw an E_NOTICE error and the script will fail in Alfred.

 

Indeed it will. Also, another good tip would be to..

 

<?php
	$tz_string = exec('systemsetup -gettimezone');
	$tz = substr( $tz_string, ( strpos( $tz_string, ": " ) + 2 ) );
?>

 

Then do

date_default_timezone_set( $tz );

 

That will grab the computers timezone and use that to set the current.

Link to comment

Indeed it will. Also, another good tip would be to..

 

<?php
	$tz_string = exec('systemsetup -gettimezone');
	$tz = substr( $tz_string, ( strpos( $tz_string, ": " ) + 2 ) );
?>

 

Then do

date_default_timezone_set( $tz );

 

That will grab the computers timezone and use that to set the current.

 

Ah, I didn't know that! Even better.

Link to comment

Took me a while to figure it out :) Since you brought it up, I knew I had to share ha.

 

 

Same for me, I was stuck for a while until I understood that the E_NOTICE/time zone caused my problems with Alfred, had to share that too :) 

Link to comment
  • 11 months later...

That's strange.

 

I still use a similar bit of code whenever I have to work with date functions, and I'm not getting that error. I call it by the full path, though. Maybe that makes a slight difference.

// Set the current timezone to prevent date errors, we'll get it from the system
$timezone = exec( '/usr/sbin/systemsetup -gettimezone' );
$timezone = str_replace( '/Time Zone: /' , '' , $timezone );
date_default_timezone_set( $timezone );

I'm on newest Mavericks, so that shouldn't be the issue.

Link to comment

I get the same error with or without the full path.

You can also get the system timezone with date +%z (e.g. +0100) date +Z (e.g. CET), but you'd have to munge those into a format PHP understands.

I think PHP still accepts the GMT+1 etc. format, but it's deprecated.

Link to comment

Here's another way:

date_default_timezone_set( exec('cat /etc/timezone') );

That one shouldn't run into any problems.

 

Whoops. That way won't work (tested it in the ubuntu shell and not the mac shell).

 

This one will:

date_default_timezone_set( str_replace( '/usr/share/zoneinfo/' , '' , readlink('/etc/localtime') ) );
Link to comment
  • 8 months later...

As an update:

 

This is what I've been using:

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

This one won't overwrite the ini setting. It seems more reliable for me now as well.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...