I have three Date Time Picker fields, one is required and the others are not. When the non-required fields are empty, they display the current date, I need them not to show if they’re not being used.
The code I have is (I’m creating a custom date format and translating it to Spanish):
$date_1 = get_field('date1', false, false);
$date1 = date_i18n('j \d\e\ F \-\ G \h\s', strtotime($date_1));
$date_2 = get_field('date2', false, false);
$date2 = date_i18n('j \d\e\ F \-\ G \h\s', strtotime($date_2));
$date_3 = get_field('date3', false, false);
$date3 = date_i18n('j \d\e\ F \-\ G \h\s', strtotime($date_3));
if ( $date1) {
echo '<h5>UPCOMING EVENTS</h5><p> ' . $date1 . " | " . $date2 . " | " . $date3 . '</p>';
}
Any suggestion?
What is being returned by get_field for these 3 fields?
if there’s no value then is should be an empty string.
$dates = array();
$date_1 = get_field('date1', false, false);
if ($date_1) {
$dates[] = date_i18n('j \d\e\ F \-\ G \h\s', strtotime($date_1));
}
$date_2 = get_field('date2', false, false);
if ($date_2) {
$dates[] = date_i18n('j \d\e\ F \-\ G \h\s', strtotime($date_2));
}
$date_3 = get_field('date3', false, false);
if ($date_3) {
$dates[] = date_i18n('j \d\e\ F \-\ G \h\s', strtotime($date_3));
}
if ( $dates) {
echo '<h5>UPCOMING EVENTS</h5><p> ' . implode(' | ', $dates) . '</p>';
}
Hi John, if I only pick date and time for the required field ($date1), it returns:
UPCOMING EVENTS
17 de agosto – 16hs | Today’s date and current time | Today’s date and current time
I’ll try with your suggestion and will report back.
Thanks.
I’ve got the same problem with some Date picker fields and it’s fixed making an if for each one.
On the other hand i would like to tell you: Lot of thanks @maira!
You helped me to fix another problem showing Date picker fields.
It was showing current date even when the fields was filled, and in another use it was rendering a different date from the one saved on the field. I was going craczy, but tried your way to make the date_i18n (different from the one in the ACF documentation) and it works perfect!
So, many thanks!
I just came across this discussion now.
Had the same problem.
It’s using the strtotime
on an empty string, that generates the current time
So for me an If check
on the initial ACF Variable fixed it
Thanks for your help 🙂
The topic ‘Empty Date Time Picker field shows current date.’ is closed to new replies.
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.