Is it safe to use ‘date_default_timezone_set’ in plugin file?

I am using a sitemaps plugin which in very complex ways sets the timezone of <lastmod> (i.e. last modified time) for posts to GMT.

Temporarily, until the plugin developer fixes it, I need to enforce a custom timezone on the plugin. The simple and straightforward way that I’ve found is to add something like date_default_timezone_set( 'America/New_York'); right after the first <?php in the file responsible for outputting the sitemaps.

It works, and it doesn’t seem to affect timestamps displayed on the rest of the site. So, can I go with this? or is there a better solution?

3 Answers
3

http://codex.wordpress.org/Function_Reference/get_gmt_from_date

Replace all instances of get_the_date or the_date with echo get_gmt_from_date(get_the_date('Y-m-d H:i:s')). That’s not a quick fix, it’s a way to fix your sitemaps.

EDIT:

WordPress SEO runs it’s raw dates from MySQL through a WP function called mysql2date, which in turn calls date_i18n. date_i18n happens to have a handy filter which you can tie into to change the date:

<?php
add_filter('date_i18n', 'wpse57195_filter_date', 10, 4);
/**
 * Changes the date on WordPress SEO's sitemaps
 *
 * @return string The Date
 */
function wpse57195_filter_date($date, $format, $timestamp, $gmt)
{
    // if this isn't a sitemap page, bail
    if(!get_query_var('sitemap'))
        return $date;

    // W3C Time format with -05:00 for central time
    // NOTE: this doesn't account for daylight saving time
    $f="Y-m-d\TH:i:s-05:00";
    return $gmt ? gmdate($f, $timestamp) : date($f, $timestamp);
}

In the hooked function, you can check for the sitemap query variable and change the time as you need. To keep with the sitemap spec, you have to use the W3C date format which includes a +/- field for timezone. -05:00 is the number for central, daylight savings time. You can wrap the above up in a plugin and activate it until the sitemap can be fixed.

Leave a Comment