Tonis
Tonis

Reputation: 9

ACF date field gives randomly wrong output

I'm using this code but the output sometimes results in 01.01.70. Clearly my code is wrong. But for the life of me I don't know where. The date comes from datefield ACF called 'datum_event'.

I have 2 different ways of showing the date on the site. It loads like l j F Y on the eventpage itself (retour format set in ACF). But I need it to show d.m.y in an event calender. (not retour format set in ACF).

It works up to the point where some months show as expected (13.04.23), but other months show again as 01.01.1970. I'm lost really.

April, June, September, November, December show fine. The other months go wrong and show as 01.01.1970.

So I end up with this code:

if ( function_exists( 'get_field' ) ) {
    $date = get_field( 'datum_event' );
        $date_parts = explode(" ", $date, 2);
    if ( isset($date_parts[1]) && is_string( $date ) ) {
        $html .= '
' . date( 'd.m.y', strtotime( $date_parts[1] ) ) . '
    ';
    }
}

Upvotes: 0

Views: 493

Answers (1)

Tonis
Tonis

Reputation: 9

if ( function_exists( 'get_field' ) ) {
$date = get_field('datum_event',false,false);

if ( isset($date) && is_string( $date ) ) {
    $html .= '' . date( 'd.m.y', strtotime( $date) ) . '
';
}

This is the solution!

Upvotes: 0

Related Questions