Support

Account

Home Forums Front-end Issues WP query date output wrong

Solved

WP query date output wrong

  • I have created a CPT called meetings and added the date field. I have created 2 posts with dates;

    24/10/2019
    11/09/2020

    This is my code;

    <?php 
        $args = array( 
        	'post_type' => 'meetings'
        );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); 
    
    	$date = get_field('date');
    
    	$month = date( "M", strtotime($date) ); 
    	$year = date( "Y", strtotime($date) ); 
    
    	// echo $date;
    	echo '<br>';
    	echo $month;
    	echo '<br>';
    	echo $year; 
    
    	?>
    
        <a href="<?php the_permalink(); ?>" class="meeting">
        	<br>
    		<?php the_field('date'); ?>
    		<br>
        </a>
        <br><br>
    <?php endwhile; ?>      
    <?php wp_reset_query(); ?>

    Yet when it outputs, it comes with as;

    Jan
    1970
    24/10/2019

    Nov
    2020
    11/09/2020

    I have no idea how it’s getting Jan 1970 and Nov 2020???

    The bottom date is correct but comes with the_field('date');

  • Because DD/MM/YYYY is not a valid date format for strtotime(). strtotime() is assuming this is MM/DD/YYYY

    https://www.php.net/manual/en/datetime.formats.date.php

  • Hi @hube2,

    I worked out it was the output format that was causing the confusion. Once I had adjusted that in the back-end it worked.

    Thanks for the help though 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.