Search code examples
phpwordpressdatetimeadvanced-custom-fieldsacfpro

Show or hide content based on ACF time/date field


How can I show and hide content at a specific time on a specific day using two ACF date/time fields? I am hosting live video events. I need to show a placeholder video until the live event begins, then show the live video between the start and end date/time (ACF date/time fields). I'll need to show the placeholder video again after the event end date/time.

I've configured ACF with a start date/time field and an end date/time field.

`<?php
$current_date = date('Y-m-d H:i:s');
$current_time = strtotime($current_date);
$start = get_field('start_date');
$end = get_field('end_date');
if ($current_time >= $start && $current_time <= $end){ 
//show content if between start and end time
echo '<h3>We are Live</h3>';
} else {
//show only before or after start & end time
echo '<h6>Not Live</h6>';
}
?>`

It seems that is is only taking the day into consideration and not the time.


Solution

  • I was able to accomplish this by converting the start & end times to UnixTime and adding a field to add or subtract hours for time zone differences.

    <?php
    $current_date = date('Y-m-d H:i:s');
    $current_time = strtotime($current_date);
    $time_zone = get_field('time_zone');
    $now = strtotime( $time_zone['value'], $current_time);
    $start = strtotime(get_field('start_date'));
    $end = strtotime(get_field('end_date'));                
    
    if ($now >= $start && $now <= $end){
      // Show  live video between start & end time
    } else {
      // Show placeholder video before and after start & end time
    }
    ?>