Support

Account

Home Forums General Issues Display day and month from date_picker field.

Solved

Display day and month from date_picker field.

  • Hello,

    I am trying to display events cpt inside artists cpt using the following code an trying to get the following appearance: Appearance, for which I require the day and the month as separate values to style them with CSS.

    How would be possible to store the data as variable to reuse it later.

    
    <!-- EVENTOS -->
    <div class="container">
    	<div class="row">
    	<div class="col-sm-12"><h2 class="seccion">Fotos</h2></div>
    	<?php
          $today = current_time('Ymd');
    
          $args = array(
            'post_type' => 'evento',
            'post_status' => 'publish',
            'posts_per_page' => '-1',
            'meta_query' => array(
              array(
                'key' => 'fecha_del_evento',
                'compare' => '>=',
                'value' => $today,
                )
              ),
            'meta_key' => 'fecha_del_evento',
            'orderby' => 'meta_value',
            'order' => 'ASC',
            );
    
          $query = new WP_Query($args);
          if ($query->have_posts()) :
            while ($query->have_posts()) : $query->the_post(); 
          ?>
          
    	      <div class="col-sm-3">
    	      	<?php
    		      	$date = DateTime::createFromFormat('dmY', get_field('fecha_del_evento'));
    				echo $date->format('d-m-Y');
    			?>
    	      </div>
    
           <?php endwhile; ?>
      <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
    
    	</div><!-- #row -->
    </div><!-- #container -->
    
  • Hi @kevin-mamaqi

    This isn’t really an ACF question. More of an general PHP question 🙂 But I’ll try and help you anyway.

    This code should do the trick:

    
    <!-- EVENTOS -->
    <div class="container">
    	<div class="row">
    	<div class="col-sm-12"><h2 class="seccion">Fotos</h2></div>
    	<?php
          $today = current_time('Ymd');
    
          $args = array(
            'post_type' => 'evento',
            'post_status' => 'publish',
            'posts_per_page' => '-1',
            'meta_query' => array(
              array(
                'key' => 'fecha_del_evento',
                'compare' => '>=',
                'value' => $today,
                )
              ),
            'meta_key' => 'fecha_del_evento',
            'orderby' => 'meta_value',
            'order' => 'ASC',
            );
    
          $query = new WP_Query($args);
          if ($query->have_posts()) :
            while ($query->have_posts()) : $query->the_post(); 
          ?>
          
    	      <div class="col-sm-3">
    	      	<?php
    		      	$datetime_string = get_field('fecha_del_evento');
    		      	$datetime = new DateTime($datetime_string);
    		      	$month = date_i18n('M', strtotime($datetime_string));
    		      	$day = $datetime_object->format("d");
    			?>
    			
    	      </div>
    
           <?php endwhile; ?>
      <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
    
    	</div><!-- #row -->
    </div><!-- #container -->
    
  • Hello Jonathan,

    It doesn’t display nothing after the code, even the footer (which comes after the content). In Addition, I think that it also require a echo $day; or echo $month; to display the values, which doesn’t work either.

  • Hi @kevin-mamaqi
    Ah okay. Yeah I didn’t test the code live so it’s possible some small error in my writing is causing this. You’re getting an fatal error so something is wrong with the syntax rather than the way the code works.

    Please activate wp_debug and check your errors and you should be able to find the error easily.

  • Hello @jonathan

    Thanks, I did it and got this message:

    Fatal error: Uncaught exception ‘Exception’ with message ‘DateTime::__construct():
    Failed to parse time string (17/07/2015) at position 0 (1):
    Unexpected character’ in /home/eurovia1/public_html/wildlion/wp-content/themes/wildlion/single-artista.php:207
    Stack trace: #0 /home/eurovia1/public_html/wildlion/wp-content/themes/wildlion/single-artista.php(207): DateTime->__construct(’17/07/2015′)
    #1 /home/eurovia1/public_html/wildlion/wp-includes/template-loader.php(74): include(‘/home/eurovia1/…’)
    #2 /home/eurovia1/public_html/wildlion/wp-blog-header.php(16): require_once(‘/home/eurovia1/…’)
    #3 /home/eurovia1/public_html/wildlion/index.php(17): require(‘/home/eurovia1/…’)
    #4 {main} thrown in /home/eurovia1/public_html/wildlion/wp-content/themes/wildlion/single-artista.php on line 207

    So I searched for a solution and came up with this code:

    <?php
    				$datetime_string = get_field('fecha_del_evento');
    		      	$datetime = DateTime::createFromFormat('d/m/Y', '$datetime_string');
    		      	$month = date_i18n('M', strtotime($datetime));
    		      	$day = date_i18n('d', strtotime($datetime));
    			?>
    			<?php echo $day; ?>
    			<?php echo $month; ?>

    The code returns the current day, while changing date_i18n to date returns 01-Jan. From here I’m a bit blocked.

  • Hi,

    Try this:

    
    <?php
      	$datetime_string = get_field('fecha_del_evento');
      	$datetime = new DateTime(date('Y-m-d', strtotime($datetime_string)));
      	echo $month = date_i18n('M', strtotime($datetime_string));
      	echo $day = $datetime_object->format("d");
    ?>
    

    It is possible that you need to change the way you save the date values in DB. d/m/Y is really not optimal for datetime interactions and you should save it as Ymd.

  • Hi,

    It returns the following message:

    jul
    Notice: Undefined variable: datetime_object in /home/eurovia1/public_html/wildlion/wp-content/themes/wildlion/single-artista.php on line 209 Fatal error: Call to a member function format() on a non-object in /home/eurovia1/public_html/wildlion/wp-content/themes/wildlion/single-artista.php on line 209

    Line 209 -> echo $day = $datetime_object->format(“d”);

  • I think I got it, or at least it is working now. I changed into the datepicker field options from 14/09/2915 to d/m/Y (I wrote it manually, I don’t know why selecting the 14/09/2015 option didn’t work).

    And now using this code is working fine:

    <div class="col-sm-3">
    
    			<?php 
    			if(get_field('fecha_del_evento'))
    			{
    				$datetime = DateTime::createFromFormat('d/m/Y', get_field('fecha_del_evento'));
    				$fecha_dia = $datetime->format('d');
    				$fecha_mes = $datetime->format('M');
    			}            
    			?>
    			<div class="eventos row">
    				<div class="col-xs-3">
    					<div class="fecha">
    						<p class="mes"><?php echo $fecha_mes; ?></p>
    						<p class="dia"><?php echo $fecha_dia; ?></p>
    					</div>
    				</div>
    				<div class="col-xs-9">
    					<?php the_content (); ?>
    				</div>
    			</div>
    		</div>
  • Great 🙂

    You should be able to still save the format as Ymd and just change the createFromFormat setting.

  • changing the return format to “ymd” worked for me, thanks guys!

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

The topic ‘Display day and month from date_picker field.’ is closed to new replies.