Support

Account

Forum Replies Created

  • Hi Elliot,

    I think I found my solution. I found it at the bottom of this thread:
    http://support.advancedcustomfields.com/forums/topic/datepicker-wrong-format-is-displaying/

    I changed my code as follows:

    $date = DateTime::createFromFormat('Ymd', get_field('class_date'));
    echo '<p>' . $date->format('F j, Y') . '</p>';

    I changed ‘yymmdd’ to ‘Ymd’ and then changed the format value from ‘F n Y’ to ‘F j, Y’.

    I’m now confused because the documentation tells us to use the save format yymmdd and since we are using the Jquery picker, to format using the JS Date Format. I never thought to try PHP Date format and it worked. Maybe I don’t fully understand the documentation, but this is where some of my confusion lies. Anyway, the code seems to be pulling the correct date and formatting the way I configured it to.

    Thank you for your help!
    Kim

  • Hi Elliot,

    I was able to figure out how to debug the value. When I added the 2nd set of debug code, it pulled the correct date (20131129) but it’s not formatting correctly.

    I tried adding the $date variable debug code and got a blank white screen. Not sure if I’m doing this correctly. It seems it’s having an issue with the formatting of the date.

    Any ideas?

  • Hi Elliot,

    How exactly should I use this debugging code? Sorry, PHP newbie, here.

    Kim

  • Hi Elliot,

    In the documentation, it says to call the $date as such:

    echo $date->format('d-m-Y');

    When I use this to call the $date, I get a “Fatal error: Call to a member function format() on a non-object…”

    Here’s what I have

    <?php
    remove_action('genesis_loop', 'genesis_do_loop');
    add_action('genesis_loop', 'custom_loop');
    
    function custom_loop() {  
      	
    	// Intro Text (from page content)
    	echo '<div class="page hentry entry">';
    	echo '<h1 class="entry-title">'. get_the_title() .'</h1>';
    	echo '<div class="entry-content">' . get_the_content() ;
    	
        
    	$args = array(
    		'post_type' => 'events', // enter your custom post type
    		'meta_key' => 'class_date', // name of custom field
    	    'orderby' => 'meta_value_num',
    	    'order' => 'ASC',
    	);
    	
    
    	$loop = new WP_Query( $args );
    	if( $loop->have_posts() ):
    	
    					
    	while( $loop->have_posts() ): $loop->the_post(); global $post;
    	
        echo '<div id="events">';
    			
    			echo '<h3>' . get_the_title() . '</h3>';
    			
    	/* Create PHP DateTime object from Date Picker Value
        this example expects the value to be saved in the format: yymmdd (JS) = Ymd (PHP) */
    
        $date = DateTime::createFromFormat('yymmdd', get_field('class_date'));
    			
    			echo '<p>' . $date->format('F n Y') . '</p>';
    			echo '<p>'. get_field('time') . '</p>';
    			echo '<p>' . get_the_content() . '</p>';
            echo '</div>';
    		
    		endwhile;
    		
    	endif;
    	
    			
    }
    	
    /** Remove Post Info */
    remove_action('genesis_before_post_content','genesis_post_info');
    remove_action('genesis_after_post_content','genesis_post_meta');
     
    
    genesis(); 
    ?>
  • Hi Elliot,

    As you suggested, I’moved the $date formatting to be within the loop. I’m still pulling the wrong date for each post. For some reason it seems to be pulling the wrong date for each post. I have no idea where it’s getting this date from since none of the posts have this date. Below is my code and live link:

    http://www.mimismanners.blueturtlegraphics.com/classes/upcoming-classes/

    <?php
    remove_action('genesis_loop', 'genesis_do_loop');
    add_action('genesis_loop', 'custom_loop');
    
    function custom_loop() {  
      	
    	// Intro Text (from page content)
    	echo '<div class="page hentry entry">';
    	echo '<h1 class="entry-title">'. get_the_title() .'</h1>';
    	echo '<div class="entry-content">' . get_the_content() ;
    	
        
    	$args = array(
    		'post_type' => 'events', // enter your custom post type
    		'meta_key' => 'class_date', // name of custom field
    	    'orderby' => 'meta_value_num',
    	    'order' => 'ASC',
    	);
    	
    
    	$loop = new WP_Query( $args );
    	if( $loop->have_posts() ):
    	
    					
    	while( $loop->have_posts() ): $loop->the_post(); global $post;
    	
    
    	
    		
    		echo '<div id="events">';
    			
    			echo '<h3>' . get_the_title() . '</h3>';
    			
    				/* Create PHP DateTime object from Date Picker Value
        this example expects the value to be saved in the format: yymmdd (JS) = Ymd (PHP) */
    
        $date = DateTime::createFromFormat('yymmdd', get_field('class_date'));
    			
    			echo '<p>' . date('F n Y') . '</p>';
    			echo '<p>'. get_field('time') . '</p>';
    			echo '<p>' . get_the_content() . '</p>';
            echo '</div>';
    		
    		endwhile;
    		
    	endif;
    	
    			
    }
    
    	
    /** Remove Post Info */
    remove_action('genesis_before_post_content','genesis_post_info');
    remove_action('genesis_after_post_content','genesis_post_meta');
     
    
    genesis(); 
    ?>
  • Ok, I’ve changed my template code and have now managed to get the date picker and text field for time to display. My only issue now is that the dates are shown as 20131129 (yymmdd) and I want them to display as “F n Y” or November 29, 2013. I’ve tried adding in the code:

    echo date('F n Y', $time);

    but I keep getting an error:

    Fatal error: Call to a member function format() on a non-object in /home/mimismanners/mimismanners.com/wp-content/themes/dynamik-gen/my-templates/upcomingclasses.php on line 32

    HERE’S MY TEMPLATE CODE

    <?php
    /*
     * Template Name: Upcoming Classes
     */
    ?>
    
    <?php
    remove_action('genesis_loop', 'genesis_do_loop');
    add_action('genesis_loop', 'custom_loop');
    
    function custom_loop() {  
      	
    	// Intro Text (from page content)
    	echo '<div class="page hentry entry">';
    	echo '<h1 class="entry-title">'. get_the_title() .'</h1>';
    	echo '<div class="entry-content">' . get_the_content() ;
    	
        
    	$args = array(
    		'post_type' => 'events', // enter your custom post type
    		'meta_key' => 'class_date', // name of custom field
    	    'orderby' => 'meta_value_num',
    	    'order' => 'ASC',
    	);
    	
    
    /* Create PHP DateTime object from Date Picker Value
        this example expects the value to be saved in the format: yymmdd (JS) = Ymd (PHP) */
    
        $date = DateTime::createFromFormat('yymmdd', get_field('class_date'));
        echo $date->format('mmddyy');
        echo date('F n Y', $time);
    
    	$loop = new WP_Query( $args );
    	if( $loop->have_posts() ):
    	
    					
    	while( $loop->have_posts() ): $loop->the_post(); global $post;
    	
    		
    		echo '<div id="events">';
    			
    			echo '<h3>' . get_the_title() . '</h3>';
    			echo '<p>' . get_field('class_date') . '</p>';
    			echo '<p>'. get_field('time') . '</p>';
    			echo '<p>' . get_the_content() . '</p>';
            echo '</div>';
    		
    		endwhile;
    		
    	endif;
    	
    			
    }
    
    	
    /** Remove Post Info */
    remove_action('genesis_before_post_content','genesis_post_info');
    remove_action('genesis_after_post_content','genesis_post_meta');
     
    
    genesis(); 
    ?>

    Any help would me much appreciated!!!

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