Conditional: IF post creation date is in the first half OR last half of the year

admin2025-06-05  1

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I'm trying to find a way how to have a simple conditional whereby I can echo something depending on whether the post date is in the first half or last half of the post date year.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I'm trying to find a way how to have a simple conditional whereby I can echo something depending on whether the post date is in the first half or last half of the post date year.

Share Improve this question asked Dec 7, 2018 at 5:43 PetePete 1,0582 gold badges14 silver badges40 bronze badges 2
  • And what have you tried so far? See wordpress.stackexchange/help/how-to-ask. You're basically looking at extracting the MM out of YYYYMMDD, convert to int, and check if greater than 6. – anmari Commented Dec 7, 2018 at 7:03
  • I don't know where to start – Pete Commented Dec 7, 2018 at 12:37
Add a comment  | 

2 Answers 2

Reset to default 2

Are you thinking of something like this?

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); 
        if ( absint( get_the_date( 'n' ) ) <= 6 ) {
            // First half of year
            ...
        } else {
            // Second half of year
            ...
        }
    }
}

I found this elsewhere and it worked for me...

if( (int)get_the_time( 'm' ) <= 6 ) {
    echo 'Some time between January and June.';
} else {
    echo 'Some time between July and December.';
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749119116a316524.html

最新回复(0)