These are the functions I use in my theme to display the “entry-meta” which includes published and last modified dates (among others) of an article:
// Shows Author and Published Date
if ( ! function_exists( 'reddle_posted_on' ) ) :
function reddle_posted_on() {
printf( __( '<span class="byline">Posted by <span class="author vcard"><a class="url fn n" href="https://wordpress.stackexchange.com/questions/54700/%5$s" title="%6$s" rel="author">%7$s</a></span></span><span class="sep"> — </span><span class="entry-date"><time datetime="%3$s" pubdate>%4$s</time></span>', 'reddle' ),
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
sprintf( esc_attr__( 'View all posts by %s', 'the-wolf' ), get_the_author() ),
esc_html( get_the_author() )
);
}
endif;
// Shows Last Modified Date
if ( ! function_exists( 'reddle_last_modified_on' ) ) :
function reddle_last_modified_on() {
printf( __( 'Last updated on <time class="updated" itemprop="dateModified" datetime="%2$s">%3$s</time>', 'reddle' ),
esc_attr( get_the_modified_time() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date( 'F j, Y ~ H:i' ) )
);
}
endif;
Do you see anything wrong with these functions? The problem is, despite setting the time zone of my blog to GMT-05:00
(-04:00 DST) in WordPress Dashboard > Settings > General the timestamp that is output shows GMT+00:00
. Any idea why?
3 Answers
The issue is that for correct output WP needs to process date through date_i18n()
function. When you use date format, hardcoded in PHP code (not simply saved in PHP DATE_*
constant) like 'c'
– it’s not available to your code and so for WP to process.
System-wide fix would be to re-process date with analogous format that can be accessed by WP code:
add_filter( 'date_i18n', 'fix_c_time_format', 10, 4 );
function fix_c_time_format( $date, $format, $timestamp, $gmt ) {
if ( 'c' == $format )
$date = date_i18n( DATE_ISO8601, $timestamp, $gmt );
return $date;
}