There are a few options, none of which really work great. This is a WordPress bug, and it genuinely sucks because the time is wrong unless you set your site to UTC... which is confusing and not always even possible.
This next code I think only works if you choose your Timezone (Under Settings -> General in admin) as a named city instead of by an GMT number offset. I haven't tested this but it's very possible that get_option('gmt_offset')
is set when get_option('timezone_string')
is not.
date_default_timezone_set(get_option('timezone_string'));
The downside of this is that WordPress assumes PHP is set to UTC when making mysql timestamps, so you can mess up your database a little bit whenever you switch timezones! Not to mention other WP plugins may assume that the PHP environment is always in UTC.
So, if you just want a correct time -- you can force your timestamp to be in UTC with:
get_post_time('c', true); //should work for non-post objects.
Unfortunately, although correct, it'll make the timezone get set to UTC.
And note that you can't both use the "true" flag and the default timezone_set function.
Any proper solution is gonna be a code-snippet that accounts for both gmt_offset
AND timezone_string
and uses them to set a timezone on some input. WP assumes that PHP set to UTC when doing mysql timestamps, and it might break other plugins.
There's one such solution on https://www.skyverge.com/blog/down-the-rabbit-hole-wordpress-and-timezones/ but, again this is a BUG, so you should use the get_post_time($date_format, TRUE)
code to get a timestamp that is actually correct.