It's not yesterday here
December 3, 2005 7:15 PM Subscribe
WordPress Filter: I am trying to show the local time on my WP sidebar. I am located in Sydney, AUS but my server is in the US. I have set the the offset on the WP>Options page under "Times in the weblog should differ by: to the correct setting and posting times are correct. However, when I add the PHP function echo date("M d, h:ia") in my sidebar.php, I get the local time/date of my server. How to I offset this function call to show the correct local time here in Sydney?
I looked it up in zoneinfo for you.
posted by evariste at 7:36 PM on December 3, 2005
$timezone="Australia/Sydney"
posted by evariste at 7:36 PM on December 3, 2005
I missed the trailing ; in my second comment, but you get the idea. If you have shell access to your server, you can look timezones up by
posted by evariste at 7:38 PM on December 3, 2005
cat
ing /usr/share/zoneinfo/zone.tab
posted by evariste at 7:38 PM on December 3, 2005
I would be extremely careful about mucking around with $TZ like that. It will change the effective setting of the local time zone for that entire process from that point on. Since mod_php does not spawn a new process for each request, it means that the setting will remain set for any other requests that happen to come to that process. This means that once you do this you essentially end up with a random and unpredictable setting of $TZ because it's been set in some processes and not others. And this is on top of the potential bugginess if you call this in the middle of a script, where the first half of the script may assume one time zone, and the second half will do strange things since it expects the same $TZ.
If you are going to do it that way, I would at least save/restore the value of $TZ. So that your local setting only affects that one call to date().
It would be *much* better to just access the existing time offset setting in the WordPress configuration, and use that. If you do it that way you don't have to worry about any of the above landmines of having different environments for different PHP processes, and it means that you only have to set it in one place (the WP config), instead of configuring it in two completely unrelated locations.
posted by Rhomboid at 9:00 PM on December 3, 2005
If you are going to do it that way, I would at least save/restore the value of $TZ. So that your local setting only affects that one call to date().
It would be *much* better to just access the existing time offset setting in the WordPress configuration, and use that. If you do it that way you don't have to worry about any of the above landmines of having different environments for different PHP processes, and it means that you only have to set it in one place (the WP config), instead of configuring it in two completely unrelated locations.
posted by Rhomboid at 9:00 PM on December 3, 2005
Best answer: Rhomboid-that doesn't seem like a big issue to me, but ok...do this then:
I haven't tested this, but it probably works.
posted by evariste at 9:17 PM on December 3, 2005
$server_timezone=getenv("TZ");
$timezone="Australia/Sydney";
putenv("TZ=$timezone");
echo date("M d, h:ia");
putenv("TZ=$server_timezone");
I haven't tested this, but it probably works.
posted by evariste at 9:17 PM on December 3, 2005
Response by poster: evariste, that worked like a charm. Thanks for the tweak to unset the environment, as I did not want to screw up any other date sensitive outputs by accident, as Rhomboid so wisely pointed out.
posted by qwip at 10:17 PM on December 3, 2005
posted by qwip at 10:17 PM on December 3, 2005
Response by poster: Just to tie up lose ends, Rhomboid, do you know how to access the existing time offset setting that WordPress is using for posting? That was my original intent, although evariste's code worked well.
posted by qwip at 10:21 PM on December 3, 2005
posted by qwip at 10:21 PM on December 3, 2005
Best answer: You can probably do this without messing with environment vars at all if you want:
echo date('M d, h:ia', strtotime('+12 hours'));
where +12 hours is the actual offset between the server's reported time and the time you want to get. It obviously won't work without further processing if there is a variable difference between the timezones.
posted by moift at 11:37 PM on December 3, 2005
echo date('M d, h:ia', strtotime('+12 hours'));
where +12 hours is the actual offset between the server's reported time and the time you want to get. It obviously won't work without further processing if there is a variable difference between the timezones.
posted by moift at 11:37 PM on December 3, 2005
Response by poster: Thanks, moift. That works. Oddly, I had to use +17, to get it to offset correctly even though I am +10 from my US server. Whatever, it works and it's one line that just adds to the blog time. Good on ya.
I want to thank both you and evariste for coming up with great solutions and quickly at that. Much obliged. This is what makes AskMe great.
posted by qwip at 3:44 AM on December 4, 2005
I want to thank both you and evariste for coming up with great solutions and quickly at that. Much obliged. This is what makes AskMe great.
posted by qwip at 3:44 AM on December 4, 2005
I don't know specifically how you'd access the WP setting, because I've never looked at WP. But normally it will be stored in the DB and if WP code has already been run on the current page it will be in some global variable that you can access. If you are curious just poke around in the WP code at where it emits dates, and you'll see how it accesses the setting.
posted by Rhomboid at 9:05 PM on December 4, 2005
posted by Rhomboid at 9:05 PM on December 4, 2005
This thread is closed to new comments.
$timezone="US/Pacific";
putenv("TZ=$timezone");
echo date("M d, h:ia");
Adjust for the timezone for Sydney.
posted by evariste at 7:30 PM on December 3, 2005