Support

Account

Home Forums General Issues Filter Custom Post Type by ACF Date Picker Field Reply To: Filter Custom Post Type by ACF Date Picker Field

  • Okay, I have the posts showing now, in order of date (ascending). But the date is always “January 1 1970”. My save format is the ACF default yymmdd and my display format is the ACF default: dd/mm/yy

    Where would the January 1 1970 be coming from?

    <?php get_header(); ?>
    
    	<?php 
    	/*
    	*  Create PHP DateTime object from Date Piker Value
    	*  The ACF "Workaround" for PHP 5.2 as shown in the docs
    	*/
    	$date = get_field('event_enddate'); // $date = 19881123 (23/11/1988)
    	 
    	// extract Y,M,D
    	$y = substr($date, 0, 4);
    	$m = substr($date, 4, 2);
    	$d = substr($date, 6, 2);
    	 
    	// create UNIX
    	$time = strtotime("{$d}-{$m}-{$y}");
    	 
    	/* Order Posts based on Date Picker value */
    	$posts = get_posts(array(
    		'post_type' => 'event', // name of custom post type
    		'meta_key' => 'event_enddate', // name of custom field
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC'
    	));
    	 
    	if( $posts )
    	{
    		foreach( $posts as $post )
    		{
    			setup_postdata( $post );
    	 
    			the_title();
    			echo date('F n Y', $time);
    
    		}
    		wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
    	}
    ?>
          
    <?php get_footer(); ?>