Support

Account

Home Forums ACF PRO ACF Datepicker within an ACF Repeater row, dates not $Post unique

Solved

ACF Datepicker within an ACF Repeater row, dates not $Post unique

  • Hey All / Support,

    I have noticed that this question has been asked before:

    https://support.advancedcustomfields.com/forums/topic/loop-based-on-date-picker-inside-repeater-row/

    I have a ACF repeater field. Inside this I have a datepicker type. Prior I echo’ed out a value in a format selected in the return settings inside wp-admin. However I want to add a timestamp using the datepicker entry while trying to validate the HTML time element.

    I have attached my code, this is within the loop, it works fine. However the time is repeated, is there any idea how to get around this?

    
    <footer class="home__slide__authorDate">
    	<cite class="home__slide__cite"><?php echo strtoupper(get_sub_field('slide_author')); ?></cite>
    	<span class="hide-for-small-only hide-for-medium-only">&nbsp;&nbsp;-&nbsp;&nbsp;</span>
    
    	<?php // get raw date
    	$slide_datepicker = get_field('slide_datepicker', false, false);
    
    	// make date object
    	$slide_datepicker = new DateTime($slide_datepicker);?>
    	<time class="home__slide__date" datetime="<?php echo $slide_datepicker->format('Y-m-d'); ?>"><?php echo $slide_datepicker->format('d | m | Y'); ?></time>
    </footer>
    

    Many thanks,

    Neil

  • The date picker field only stores the date and not the time. Since the time is not stored when you create a new date the time will be set to 00:00:00. There isn’t any way to do this with the date picker field. There are some date & time pickers available for ACF but these are all 3rd party plugins.

  • Hi John,

    I think there is some confusion, I am looking for the date format only.

    The documentation points towards using “new DateTime”:

    https://www.advancedcustomfields.com/resources/date-picker/

    I can see your point of view however, I am looking to create two formats of data per repeater row item (variable created).

    Does this clear things up?

    Many Thanks,

    Neil

  • The documentation may point to using that, but ACF only stores the Date and does not store the time. The format that is stored in the DB is YYYYMMDD. You are looking to extract a time from this, which cannot be done because the information does not exist. The “Timestamp” value will always be for 00:00:00 on the date stored.

  • Hi @hube2 thanks for the timely response.

    My main data needed is just the date, so I am happy to ignore the time aspect to the data.

    How would you approach returning two values:

    <time class="home__slide__date" datetime="<?php echo $date->format('Y-m-d'); ?>"><?php echo $date->format('d | m | Y'); ?></time>

    I just want to change the order and symbols in the echo’ed areas. Do you have any ideas? Just repeating it twice is not valid in the former datetime attribute.

    Many thanks,

    Neil

  • Personally I don’t use the DateTime class, seems like too much work for me, I just use strtotime() which is still a valid function

    $date = get_field('slide_datepicker', false, false);
    $time = strtotime($date);
    echo date('Y-m-d', $time);
    echo date('d | m | Y', $time);
    
  • This reply has been marked as private.
  • This reply has been marked as private.
  • sorry, submitted it by mistake, so there are edits

    I’m not sure why you need to do this

    
    $slide_datepicker_raw = str_replace("/", "-", get_sub_field('slide_datepicker', false, false));
    

    You are indicating false for the 3rd parameter so ACF should not be formatting the value and returning the date value without the “/”.

  • Hi @hube2,

    Thanks that explains the use of false, I have done some spring cleaning. Here is the working end result.

    
    <?php  $slide_datepicker = strtotime( get_sub_field('slide_datepicker', false, false) ); ?>
    
    <time class="home__slide__date" datetime="<?php echo date('Y-m-d', $slide_datepicker); ?>"><?php echo date('d | m | Y', $slide_datepicker); ?></time>
    

    Thanks for the support!

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

The topic ‘ACF Datepicker within an ACF Repeater row, dates not $Post unique’ is closed to new replies.