Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8182779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:57:49+00:00 2026-06-07T00:57:49+00:00

Below is the code from a plugin I use for sitemaps. I would like

  • 0

Below is the code from a plugin I use for sitemaps.

  1. I would like to know if there’s a way to “enforce” a different timezone on all date variable and functions in it.

  2. If not, how do I modify the code to change the timestamp to reflect a different timezone? Say for example, to America/New_York timezone.

Please look for date to quickly get to the appropriate code blocks. Code on Pastebin.

<?php
/**
 * @package XML_Sitemaps
 */

class WPSEO_Sitemaps {

.....

    /**
     * Build the root sitemap -- example.com/sitemap_index.xml -- which lists sub-sitemaps
     * for other content types.
     *
     * @todo lastmod for sitemaps?
     */

    function build_root_map() {
        global $wpdb;

        $options = get_wpseo_options();

        $this->sitemap = '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
        $base          = $GLOBALS['wp_rewrite']->using_index_permalinks() ? 'index.php/' : '';

        // reference post type specific sitemaps
        foreach ( get_post_types( array( 'public' => true ) ) as $post_type ) {
            if ( $post_type == 'attachment' )
                continue;

            if ( isset( $options['post_types-' . $post_type . '-not_in_sitemap'] ) && $options['post_types-' . $post_type . '-not_in_sitemap'] )
                continue;

            $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' LIMIT 1", $post_type ) );
            // don't include post types with no posts
            if ( !$count )
                continue;

            $n = ( $count > 1000 ) ? (int) ceil( $count / 1000 ) : 1;
            for ( $i = 0; $i < $n; $i++ ) {
                $count = ( $n > 1 ) ? $i + 1 : '';

                if ( empty( $count ) || $count == $n ) {
                    $date = $this->get_last_modified( $post_type );
                } else {
                    $date = $wpdb->get_var( $wpdb->prepare( "SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = %s ORDER BY post_modified_gmt ASC LIMIT 1 OFFSET %d", $post_type, $i * 1000 + 999 ) );
                    $date = date( 'c', strtotime( $date ) );
                }

                $this->sitemap .= '<sitemap>' . "\n";
                $this->sitemap .= '<loc>' . home_url( $base . $post_type . '-sitemap' . $count . '.xml' ) . '</loc>' . "\n";
                $this->sitemap .= '<lastmod>' . htmlspecialchars( $date ) . '</lastmod>' . "\n";
                $this->sitemap .= '</sitemap>' . "\n";
            }
        }

        // reference taxonomy specific sitemaps
        foreach ( get_taxonomies( array( 'public' => true ) ) as $tax ) {
            if ( in_array( $tax, array( 'link_category', 'nav_menu', 'post_format' ) ) )
                continue;

            if ( isset( $options['taxonomies-' . $tax . '-not_in_sitemap'] ) && $options['taxonomies-' . $tax . '-not_in_sitemap'] )
                continue;
            // don't include taxonomies with no terms
            if ( !$wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND count != 0 LIMIT 1", $tax ) ) )
                continue;

            // Retrieve the post_types that are registered to this taxonomy and then retrieve last modified date for all of those combined.
            $taxobj = get_taxonomy( $tax );
            $date   = $this->get_last_modified( $taxobj->object_type );

            $this->sitemap .= '<sitemap>' . "\n";
            $this->sitemap .= '<loc>' . home_url( $base . $tax . '-sitemap.xml' ) . '</loc>' . "\n";
            $this->sitemap .= '<lastmod>' . htmlspecialchars( $date ) . '</lastmod>' . "\n";
            $this->sitemap .= '</sitemap>' . "\n";
        }

        // allow other plugins to add their sitemaps to the index
        $this->sitemap .= apply_filters( 'wpseo_sitemap_index', '' );
        $this->sitemap .= '</sitemapindex>';

    }

    /**
     * Build a sub-sitemap for a specific post type -- example.com/post_type-sitemap.xml
     *
     * @param string $post_type Registered post type's slug
     */
    function build_post_type_map( $post_type ) {
        $options = get_wpseo_options();

        ............

        // We grab post_date, post_name, post_author and post_status too so we can throw these objects into get_permalink, which saves a get_post call for each permalink.
        while ( $total > $offset ) {

            $join_filter  = apply_filters( 'wpseo_posts_join', '', $post_type );
            $where_filter = apply_filters( 'wpseo_posts_where', '', $post_type );

            // Optimized query per this thread: http://wordpress.org/support/topic/plugin-wordpress-seo-by-yoast-performance-suggestion
            // Also see http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/

            $posts = $wpdb->get_results( "SELECT l.ID, post_content, post_name, post_author, post_parent, post_modified_gmt, post_date, post_date_gmt
            FROM ( 
                SELECT ID FROM $wpdb->posts {$join_filter}
                        WHERE post_status = 'publish'
                        AND post_password = ''
                        AND post_type = '$post_type'
                        {$where_filter}
                        ORDER BY post_modified ASC
                        LIMIT $steps OFFSET $offset ) o
            JOIN $wpdb->posts l
                ON l.ID = o.ID
                ORDER BY l.ID" );

            /*          $posts = $wpdb->get_results("SELECT ID, post_content, post_name, post_author, post_parent, post_modified_gmt, post_date, post_date_gmt
                FROM $wpdb->posts {$join_filter}
                WHERE post_status = 'publish'
                AND post_password = ''
                AND post_type = '$post_type'
                {$where_filter}
                ORDER BY post_modified ASC
                LIMIT $steps OFFSET $offset"); */

            $offset = $offset + $steps;

            foreach ( $posts as $p ) {
                $p->post_type   = $post_type;
                $p->post_status = 'publish';
                $p->filter      = 'sample';

                if ( wpseo_get_value( 'meta-robots-noindex', $p->ID ) && wpseo_get_value( 'sitemap-include', $p->ID ) != 'always' )
                    continue;
                if ( wpseo_get_value( 'sitemap-include', $p->ID ) == 'never' )
                    continue;
                if ( wpseo_get_value( 'redirect', $p->ID ) && strlen( wpseo_get_value( 'redirect', $p->ID ) ) > 0 )
                    continue;

                $url = array();

                $url['mod'] = ( isset( $p->post_modified_gmt ) && $p->post_modified_gmt != '0000-00-00 00:00:00' ) ? $p->post_modified_gmt : $p->post_date_gmt;
                $url['chf'] = 'weekly';
                $url['loc'] = get_permalink( $p );

.............

    }

    /**
     * Build a sub-sitemap for a specific taxonomy -- example.com/tax-sitemap.xml
     *
     * @param string $taxonomy Registered taxonomy's slug
     */
    function build_tax_map( $taxonomy ) {
        $options = get_wpseo_options();

..........

            // Grab last modified date
            $sql        = "SELECT MAX(p.post_date) AS lastmod
                    FROM    $wpdb->posts AS p
                    INNER JOIN $wpdb->term_relationships AS term_rel
                    ON      term_rel.object_id = p.ID
                    INNER JOIN $wpdb->term_taxonomy AS term_tax
                    ON      term_tax.term_taxonomy_id = term_rel.term_taxonomy_id
                    AND     term_tax.taxonomy = '$c->taxonomy'
                    AND     term_tax.term_id = $c->term_id
                    WHERE   p.post_status = 'publish'
                    AND     p.post_password = ''";
            $url['mod'] = $wpdb->get_var( $sql );
            $url['chf'] = 'weekly';
            $output .= $this->sitemap_url( $url );
        }

    }

    /**
     * Build the <url> tag for a given URL.
     *
     * @param array $url Array of parts that make up this entry
     * @return string
     */
    function sitemap_url( $url ) {
        if ( isset( $url['mod'] ) )
            $date = mysql2date( "Y-m-d\TH:i:s+00:00", $url['mod'] );
        else
            $date = date( 'c' );
        $output = "\t<url>\n";
        $output .= "\t\t<loc>" . $url['loc'] . "</loc>\n";
        $output .= "\t\t<lastmod>" . $date . "</lastmod>\n";
        $output .= "\t\t<changefreq>" . $url['chf'] . "</changefreq>\n";
        $output .= "\t\t<priority>" . str_replace( ',', '.', $url['pri'] ) . "</priority>\n";
        if ( isset( $url['images'] ) && count( $url['images'] ) > 0 ) {
            foreach ( $url['images'] as $img ) {
                $output .= "\t\t<image:image>\n";
                $output .= "\t\t\t<image:loc>" . esc_html( $img['src'] ) . "</image:loc>\n";
                if ( isset( $img['title'] ) )
                    $output .= "\t\t\t<image:title>" . _wp_specialchars( html_entity_decode( $img['title'], ENT_QUOTES, get_bloginfo('charset') ) ) . "</image:title>\n";
                if ( isset( $img['alt'] ) )
                    $output .= "\t\t\t<image:caption>" . _wp_specialchars( html_entity_decode( $img['alt'], ENT_QUOTES, get_bloginfo('charset') ) ) . "</image:caption>\n";
                $output .= "\t\t</image:image>\n";
            }
        }
        $output .= "\t</url>\n";
        return $output;
    }

    /**
     * Get the modification date for the last modified post in the post type:
     *
     * @param array $post_types Post types to get the last modification date for
     * @return string
     */
    function get_last_modified( $post_types ) {
        global $wpdb;
        if ( !is_array( $post_types ) )
            $post_types = array( $post_types );

        $result = 0;
        foreach ( $post_types as $post_type ) {
            $key  = 'lastpostmodified:gmt:' . $post_type;
            $date = wp_cache_get( $key, 'timeinfo' );
            if ( !$date ) {
                $date = $wpdb->get_var( $wpdb->prepare( "SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = %s ORDER BY post_modified_gmt DESC LIMIT 1", $post_type ) );
                if ( $date )
                    wp_cache_set( $key, $date, 'timeinfo' );
            }
            if ( strtotime( $date ) > $result )
                $result = strtotime( $date );
        }

        // Transform to W3C Date format.
        $result = date( 'c', $result );
        return $result;
    }
}

global $wpseo_sitemaps;
$wpseo_sitemaps = new WPSEO_Sitemaps();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:57:50+00:00Added an answer on June 7, 2026 at 12:57 am

    I believe you’re looking for the date_default_timezone_set() function, which will force all date functions, including date(), to use the specified time zone.

    date_default_timezone_set( 'America/New_York');
    

    For your specific case to only have changes apply to the plugin, you should save the current timezone with date_default_timezone_get(), then set your own timezone, and revert it when you’re done. So, you could do something like this:

    class WPSEO_Sitemaps {
    
        private $old_timezone = '';
        private $new_timezone = 'America/New_York';
    
        function setTimezone() {
            $this->old_timezone = date_default_timezone_get();
            date_default_timezone_set( $this->new_timezone);
        }
    
        function revertTimezone() {
            date_default_timezone_set( $this->old_timezone);
        }
    
        function foo() {
            $this->setTimezone();
            date();
            $tihs->revertTimezone();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is the code from a plugin for Joomla. It works on it's own
I don't know where I've got a mistake, when I fire below code from
As you can see from the code below we, use TinyMce a lot in
I got below code from http://msdn.microsoft.com/en-us/library/dd584174(office.11).aspx for adding custom property in webpart tool pane.
I am creating a random ID using the below code: from random import *
HI: Below code is from RequestFactoryEditorDriver: /** * Returns a new array containing the
Below is the code from internalRegister method of GCMRegistrar class static void internalRegister(Context context,
Below is my code from my activity. I call setKey() to set the key,
Using the code below (from a console app I've cobbled together), I add seven
I have the code below: from the getData function im trying to call get_xml

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.