Hey All / Support,
I have noticed that this question has been asked before:
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"> - </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>
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.
I think there is some confusion, I am looking for the date format only.
The documentation points towards using “new DateTime”:
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?
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.
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);
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 “/”.
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!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!