Hi I have inherited a site and recently upgraded to ACF Pro 5.5.2 from an old version (was still using 4.4.11) and I’m currently rewriting a lot of the code.
I’m having an issue with displaying the proper date on my events. In the back end it shows the correct date in the fields, but when viewing on the page it shows January 1 1970.
The current (old) code to display the date is as follows:
while (has_sub_field('dates')) {
if(get_sub_field('event_date')) {
if ($i == 0) {
$event_date = get_sub_field('event_date');
$first_date = date('F j', $event_date);
$new_time = date('ga', $event_date);
echo $first_date;
}
if($i == ($num - 1)) {
$event_date = get_sub_field('event_date');
$last_date = date('j', $event_date);
$last_date_compare = date('F j', $event_date);
if($first_date != $last_date_compare){
echo ' - ' . $last_date;
}
}
}
$i++;
}
I checked the field date format, and it should work, but still doesn’t display the proper date & time.
Hi @mpayumo
I think there’s something wrong with the formatting. Could you please let me know the value of your Return Format option?
But you can also set the formatting to false in your code so that you will get a consistent output. Here’s an example for this case:
// get the raw value
$event_date = get_sub_field('event_date', false, false);
// convert it to a date object
$event_date = date_create_from_format( 'Ymd', $event_date );
// set the format
$first_date = $event_date->format('F j');
$new_time = $event_date->format('ga');
$last_date = $event_date->format('j');
$last_date_compare = $event_date->format('F j');
// show it
echo $first_date . ' - ' . $last_date;;
I hope this helps 🙂
I actually solved it after a few forum reads:
while (has_sub_field('dates')) {
if(get_sub_field('event_date')) {
if ($i == 0) {
$event_date = get_sub_field('event_date', false, false);
$event_date = new DateTime($event_date);
$new_time = $event_date->format('ga');
$first_date = $event_date->format('F j');
echo $first_date;
}
if($i == ($num - 1)) {
$last_date = $event_date->format('j');
$last_date_compare = $event_date->format('F j');
if ($first_date != $last_date_compare) {
echo ' - ' . $last_date;
}
}
}
$i++;
}
have to add the ‘format’ line.
The topic ‘ACF Pro Dates Showing Jan 1 1970’ 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.