• Resolved bananarama

    (@bananarama)


    Instead of the normal date / time, I want to display the season in which a post was made. So for instance, march-may = spring (03-05).

    I found this topic, but the plugin is no longer accessible.

    I know it should be really simple to do, but I just can’t hack it. I’ve tried:

    <?php if ( '3' == date('n') ) : ?>
         <p>It's March</p>
    <?php else: ?>
         <p>Not February</p>
    <?php endif; ?>

    Which detects the current month, rather than the post month. But when I try to combine with the post date it doesn’t go:

    <?php if ($post_date( '3' == date('n') ) ) : ?>
         <p>It's March</p>
    <?php else: ?>
         <p>Not February</p>
    <?php endif; ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You just have your IF condition turned around ?? date() takes a second argument, which could be a post date. When there’s no argument, the current time is used. Thus like so using $post_date:
    <?php if ( '3' == date('n', $post_date) ) : ?>

    Instead of ‘n’ for the first argument, you could use ‘z’ to get the days into the year, giving you exact control of when the season starts. I believe Spring starts on the 79th day of the year for example. Only if you’re inclined to be pedantic though ??

    If you’re really pedantic, the day can shift a bit depending on the year and one’s time zone ??

    Thread Starter bananarama

    (@bananarama)

    Closer, but still no cigar :/

    <?php if ( '1' == date('n', $post_date) ) : ?>
         Winter
    <?php else: ?>
         Not Winter
    <?php endif; ?>

    This is what I updated it to – the trouble is it’s now printing Winter for everything, not just January.

    Thank you so much by the way.

    Moderator bcworkz

    (@bcworkz)

    Any improvement is better than none ??

    The code is correct, I even tested it to be sure I didn’t miss some small detail. The problem now would likely be in how $post_date is assigned, it’s apparently not being assigned the date of the current post correctly. Assuming this code occurs inside the standard WP loop, try this variant which takes $post_date out of the picture:
    <?php if ( '1' == get_post_time('n') ) : ?>

    Thread Starter bananarama

    (@bananarama)

    Definitely! I will keep playing with it – you got me over my block ?? Thanks for all your help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Date by season / quarter’ is closed to new replies.