• Resolved Pete

    (@perthmetro)


    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.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You could use the following:

    global $post;
    
    if( (int)date( 'm', strtotime( $post->post_date ) ) <= 6 ) {
    	_e( 'Some time between January and June.' );
    } else {
    	_e( 'Some time between July and December.' );
    }

    The above converts the current post date to a timestamp then uses PHP date formatting to return a month number ( string ) which we cast to an integer. If the month number if less than or equal to 6 we’re in the first half of the year, otherwise we’re looking a post on the latter half of the year.

    Thread Starter Pete

    (@perthmetro)

    This needs to go inside my loop. I tried it but it didn’t work?

    Thread Starter Pete

    (@perthmetro)

    Can the mods please do something to stop my posts getting caught up as spam. It’s been happening for years.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    @perthmetro: It seems you were put on “modwatch” for a review that stated “See problem here…”. In effect, you were using reviews for support. A review should be your experience with a plugin/theme, not a way of leveraging support.

    I’ll remove the flag.

    I’ve also tried this in my loop and it works as expected. If you’re already in The Loop you don’t need the global $post variable. It may be beneficial to post the whole loop so we can attempt to replicate it.

    Thread Starter Pete

    (@perthmetro)

    I’m using a theme that inserts php via a backend UI (GeneratePress) so it’s a bit hard to show it. The got it placed just before the_content().

    Maybe instead of using the $post object you can use a template function get_the_time() and we’ll remove the localization functions:

    if( (int)get_the_time( 'm' ) <= 6 ) {
    	echo 'Some time between January and June.';
    } else {
    	echo 'Some time between July and December.';
    }
    Thread Starter Pete

    (@perthmetro)

    Ok I’ve got this…

    if( (int)date( 'm', strtotime( $post->post_date ) ) <= 6 ) {
    	_e( 'Semester 2' );
    } else {
    	_e( 'Semester 1' );
    }

    and only semester 2 is firing off, despite different dates

    Thread Starter Pete

    (@perthmetro)

    Thanks heaps, the get_the_time worked a treat!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Conditional: IF post creation date is in the first half OR last half of the year’ is closed to new replies.