Adrian Posted March 31, 2013 Share Posted March 31, 2013 (edited) 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. Edited March 31, 2013 by Adrian Link to comment
jdfwarrior Posted March 31, 2013 Share Posted March 31, 2013 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. R4z3r 1 Link to comment
Adrian Posted April 1, 2013 Author Share Posted April 1, 2013 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
jdfwarrior Posted April 1, 2013 Share Posted April 1, 2013 Ah, I didn't know that! Even better. Took me a while to figure it out Since you brought it up, I knew I had to share ha. Link to comment
Adrian Posted April 1, 2013 Author Share Posted April 1, 2013 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
Adrian Posted March 12, 2014 Author Share Posted March 12, 2014 I don't know when it happened, but systemsetup -gettimezone stopped working, it now returns "You need administrator access to run this tool... exiting!" Link to comment
rice.shawn Posted March 12, 2014 Share Posted March 12, 2014 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
deanishe Posted March 20, 2014 Share Posted March 20, 2014 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
rice.shawn Posted March 21, 2014 Share Posted March 21, 2014 Here's another way: date_default_timezone_set( exec('cat /etc/timezone') ); That one shouldn't run into any problems. Link to comment
rice.shawn Posted March 21, 2014 Share Posted March 21, 2014 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') ) ); poer 1 Link to comment
rice.shawn Posted November 28, 2014 Share Posted November 28, 2014 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now