Skip to main content

Print an episode’s Recorded on date in a theme template

Free Beginner

The episode editor’s Recorded on field is for your own records: Benecaster shows it nowhere on your site and no feed tag carries it. If you want it on the episode page, a theme template can read it from the _benecaster_recorded_date post meta and print it.

The value is a calendar date in the form YYYY-MM-DD, not a moment in time, and is empty when none is set. Never format it with wp_date() or strtotime(). Both read the string as midnight UTC, and wp_date() then converts it to the site’s timezone, so on any site behind UTC it prints the day before. Read the date in UTC and format it with date_i18n( …, true ): the timestamp is midnight UTC of the intended day, and the true (GMT) flag stops any offset being applied.

Code

<?php
$raw = get_post_meta( get_the_ID(), '_benecaster_recorded_date', true );
if ( preg_match( '/^\d{4}-\d{2}-\d{2}$/', (string) $raw ) ) {
    $day = DateTimeImmutable::createFromFormat( '!Y-m-d', $raw, new DateTimeZone( 'UTC' ) );
    printf(
        '<p class="recorded-on">%s</p>',
        esc_html( sprintf(
            /* translators: %s: the date the episode was recorded */
            __( 'Recorded %s', 'my-theme' ),
            date_i18n( get_option( 'date_format' ), $day->getTimestamp(), true )
        ) )
    );
}

View on GitHub →

Need this built rather than just documented? See our services →