Create schedule fields
Add these fields to the post type that controls the event, reception, or form.
- reception_start_date
- Date / start date
- reception_start_time
- Time / start time
- reception_end_date
- Date / end date
- reception_end_time
- Time / end time
Practical example 03
Use Date and Time fields for a start and end schedule, then switch template output between upcoming, active, and ended states.
Add these fields to the post type that controls the event, reception, or form.
Add this to your theme functions.php or a site-specific plugin. It compares field values using the WordPress timezone setting.
<?php
function my_cfs_datetime_timestamp( $date_field, $time_field, $post_id = null ) {
$date = CFS()->get( $date_field, $post_id, array( 'format' => 'raw' ) );
$time = CFS()->get( $time_field, $post_id, array( 'format' => 'raw' ) );
if ( empty( $date ) || empty( $time ) ) {
return null;
}
$datetime = DateTimeImmutable::createFromFormat(
'Y-m-d H:i',
$date . ' ' . $time,
wp_timezone()
);
return $datetime ? $datetime->getTimestamp() : null;
}
function my_cfs_datetime_label( $date_field, $time_field, $post_id = null ) {
$timestamp = my_cfs_datetime_timestamp( $date_field, $time_field, $post_id );
if ( ! $timestamp ) {
return '';
}
return wp_date( 'F j, Y (D) g:i a', $timestamp, wp_timezone() );
}
function my_cfs_schedule_status( $post_id = null ) {
$now = ( new DateTimeImmutable( 'now', wp_timezone() ) )->getTimestamp();
$start = my_cfs_datetime_timestamp( 'reception_start_date', 'reception_start_time', $post_id );
$end = my_cfs_datetime_timestamp( 'reception_end_date', 'reception_end_time', $post_id );
if ( $start && $now < $start ) {
return 'before';
}
if ( $end && $now >= $end ) {
return 'ended';
}
return 'active';
}<?php
$status = my_cfs_schedule_status( get_the_ID() );
$start_label = my_cfs_datetime_label(
'reception_start_date',
'reception_start_time',
get_the_ID()
);
$end_label = my_cfs_datetime_label(
'reception_end_date',
'reception_end_time',
get_the_ID()
);
?>
<section class="reception-box">
<?php if ( 'before' === $status ) : ?>
<p>
Registration opens at
<?php echo esc_html( $start_label ); ?>.
</p>
<?php elseif ( 'ended' === $status ) : ?>
<p>
Registration ended at
<?php echo esc_html( $end_label ); ?>.
</p>
<?php else : ?>
<p>
Registration is open until
<?php echo esc_html( $end_label ); ?>.
</p>
<?php echo do_shortcode( '[contact-form-7 id="123" title="Registration"]' ); ?>
<?php endif; ?>
</section>The display changes when WordPress renders the page. If a page cache or CDN serves old HTML, visitors may see the previous state after the scheduled time.