0

Question

How can I display only the Month and Year that a page was created?

Background

I have a page that will get changes has a field that shows the month and year. It needs to be in Month and year format like "December 2013"

Code

I previously had this in my template, to display (for example) December 2013:

<time><?php the_time('F Y') ?>:</time>

But since it will only show up on a specific page I wrote this

<?php 
    if ( is_page('about/mission-statement/')) {
        $post_month_year = get_post_time(get_option('date_format'), false, $post, true);
        echo '<time>' . $post_month_year . ':' . '</time>';
    }
?>

But that gives me the date with the day in addition, December 12, 2013.

So the problem is that variable. I thought I could just do something like

$post_month_year = 'the_time('F Y’)';

I just need the month and year. What did I do wrong and how can I best address this?

0

This worked for me. I created variables for both the month of the post and the year of the post.

<?php
    $post_month  = get_the_time('F');
    $post_year = get_the_time('Y'); 
    if ( is_page('about/mission-statement/')) {
        echo '<time>' . $post_month . ' ' . $post_year . ':' . '</time>';
    }
?>
  • You could simplify it: $post_date = get_the_time( 'F Y' ); will get the date in Month Year format. Then you could simply echo "<time>$post_date:</time>"; later in your code. – Pat J Sep 12 '17 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.