0
votes

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.

2

2 Answers

0
votes

I would try using WP_Query and use meta_query arrays with 'relation'

$query = new WP_Query ( array (
meta_query => array(
  'relation' => 'AND',
array (
  'key' => 'start_date',
  'compare' => '>=',
  'value'.  => $current_time,
  'type' => 'TIME',
),
 array (
  'key' => 'end_date',
  'compare' => '<=',
  'value'.  => $current_time,
  'type' => 'TIME',
)
)
));
 if ( $query->have_posts() ) { while( $query->have_posts() ) {
$query->the_post();
// Your Stuff Here
}
}
0
votes

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
}
?>