Archived topic
Change "Published Date" format
11 replies · Started by Ian on January 30, 2019
Howdy, I am hoping to change the format that my date shows in. I would like it to look like
Month Date, Year @ HH:MM AM/PM
Is this possible? Also, would it be possible to make it the same size as all the other paragraph text?
Thanks again you guys are awesome!!
Hi there,
Give this function a shot:
add_filter( 'generate_post_date_output', function() {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s @ %5$s</time> ';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">%4$s @ %6$s</time> ' . $time_string;
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() ),
esc_html( get_the_date( 'g:i A' ) ),
esc_html( get_the_modified_date( 'g:i A' ) )
);
return sprintf( '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
)
);
} );
Adding PHP: https://docs.generatepress.com/article/adding-php/
Then add this CSS:
.entry-meta {
font-size: 100%;
}
Ok I added these to the same code snippet with the PHP up above and the CSS at the bottom. This is what it is outputting now:
23 January 2019 @ 6:05 PM
Hopefully we can make it:
January 23, 2019 @ 6:05 PM.
Also, would it be possible to put the word "Published:" before the date/time as well?
Thanks again for all the help!! I really appreciate everything you've done for us!!
add_filter( 'generate_post_date_output', function() {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s @ %5$s</time> ';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">%4$s @ %6$s</time> ' . $time_string;
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() ),
esc_html( get_the_date( 'g:i A' ) ),
esc_html( get_the_modified_date( 'g:i A' ) )
);
return sprintf( '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
)
);
} );
add_action( 'wp_head', function () { ?>
<style>
.entry-meta {
font-size: 100%;
}
</style>
<?php } );
Try this instead:
add_filter( 'generate_post_date_output', function() {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s @ %5$s</time> ';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">%4$s @ %6$s</time> ' . $time_string;
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date( 'F j, Y' ) ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date( 'F j, Y' ) ),
esc_html( get_the_date( 'g:i A.' ) ),
esc_html( get_the_modified_date( 'g:i A.' ) )
);
return sprintf( '<span class="posted-on">Published: %1$s</span>', // WPCS: XSS ok, sanitization ok.
sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
)
);
} );
Let me know :)
Well once again you guys knock it out of the park!! Thanks for all the help here.
Glad I could help :)
Is there a new way to do this with the newest/current GP and GPP (also using GBP if there's a method there), or is the above code still the easiest way?
I set up a custom Content Template block, but I'm trying to modify the date displayed to just "May 2021", for example. TIA.
Hi Sam,
I believe there's a filter for content template block post output.
Try this out:
add_filter( 'generate_dynamic_element_text_output', function($post_date, $block){
$post_date = sprintf(
'<time class="entry-date published" datetime="%1$s">%2$s</time>',
esc_attr( get_the_date( 'c', $id ) ),
esc_html( get_the_date( 'F j, Y', $id ) )
);
return $post_date;
},15,2);
Or change F j, Y to your preferred date format.
Hmm. That changed all my dynamic content into dates: https://a.cl.ly/7KuogQWg
ah my bad I missed a condition.
We should be checking the dynamic text type first:
Can you try this?
add_filter( 'generate_dynamic_element_text', function( $text, $block ) {
$text_type = $block['attrs']['gpDynamicTextType'];
if ( ! empty( $text_type ) && 'post-date' === $text_type ) {
$text = sprintf(
'<time class="entry-date published" datetime="%1$s">%2$s</time>',
esc_attr( get_the_date( 'c', $id ) ),
esc_html( get_the_date( 'F j, Y', $id ) )
);
}
return $text;
}, 10, 2 );
That did the trick! Thank you.
No problem. Glad it works for you. :)