• I am using WordPress version 5.4.

    I have a problem with the dates in a CPT, and it is that the dates show one day less.

    Example: If I put in the backend the date, 08/18/20, in the frontend it shows me 08/17/20

    I think for some reason the date_i18n function is not executing correctly.

    This is the code where I show the dates:

    `<time><?php if (isset($event_from_date) && $event_from_date <> ”) echo date_i18n(get_option(‘date_format’), strtotime($event_from_date)) . ‘,’; ?></time>

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @tanjar,

    Replace your code

    <time><?php if (isset($event_from_date) && $event_from_date <> ”) echo date_i18n(get_option(‘date_format’), strtotime($event_from_date)) . ‘,’; ?></time>

    with

    <time>
    <?php
    // your_meta_key replace with post meta key
    $event_from_date = get_post_meta( $post->ID, 'your_meta_key', true );
    
    if ( isset( $event_from_date ) && !empty( $event_from_date ) ) {
    	echo date_i18n(get_option('date_format'), strtotime( $event_from_date ));
    }
    ?>
    </time>

    I checked date_i18n() and it is working fine for me

    <time>
    <?php
    $event_from_date = time();
    
    if ( isset( $event_from_date ) && !empty( $event_from_date ) ) {
     echo date_i18n(get_option('date_format'), strtotime( $event_from_date ));
    }
    ?>
    </time>

    check: time()

    Thread Starter tanjar

    (@tanjar)

    Hi @mangeshnadekar15 Thanks for you reply.

    The code you recommend is the same as the one I already have.

    The date_i18n function is not doing the job, instead if I change it to date () it shows the correct dates, but not the required language

    Hi @tanjar,

    Yes, we can use date() function, also WordPress date_i18n() function is giving me same result just like PHP date() function.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘WordPress wrong dates bug’ is closed to new replies.