I use ACF pro for my wordpress.
My datepicker field is a repeater field.
I need to return only the day.
my code :
<?php
if( have_rows('dates') ):
while ( have_rows('dates') ) : the_row();
echo get_sub_field('date')."</br>";
endwhile;
else :
echo __( 'No dates available.','mywebsite' );
endif;
?>
3
Answers
The get_sub_field() function would typically return a string depending on the field type. This instance it will return a timestamp as as string eg ‘2021-06-28 10:28:00’
If you want to return the day only you could use PHP’s strtotime function which will then return you a datetime epoch integer – this can then be used in conjunction with php’s date function to print as the day. Here is a list of the formats you can use for the date function : PHP DateTime::format
Example :
As another solution which is within your setup rather than your code – change the ‘Return Format’ of the actual field.
See : ACF Date Time Picker Docs
Examples would be :
Then your original code will output the correct format :
You can set the return value of the date field.
Ok, so if you want to display two dates, one full and one is only day, you first need the return value to be the full date, for this example lets day it d/m/Y
This will get you the result you need.
See DateTime::createFromFormat for more information about the method