I’m trying to get content to only show on a release date and time based on my GMT offset (Los Angeles).
Currently, I’m able to show/hide the content using just the date picker, but I want more specificity/granularity with time, esp. related to my GMT.
Here is the functional date only difference which works:
// SIMPLE DATE M/D/Y
$publish_date = date_create( get_sub_field('publish_date') );
$current_date = date_create( date('Y-m-d') );
$diff = date_diff( $publish_date, $current_date );
$difference = $diff->format("%R%a");
<?php if ( $difference >= 0 ) { ?>
<iframe src="<?php echo $video_url; ?>" frameborder="0" allow="autoplay; fullscreen" allowfullscreen=""></iframe>
<?php } else { ?>
<iframe src="<?php echo $teaser_url; ?>" frameborder="0" allow="autoplay; fullscreen" allowfullscreen=""></iframe>
<?php } ?>
This is what I’ve tried, but white screens my site:
// DATE + TIME M/D/Y
$publish_date_time = DateTime( get_sub_field('publish_date_time') );
$current_date_time = DateTime( date('Y-m-d H:i:s') );
$diffdt = date_diff( $publish_date_time, $current_date_time );
$differencedt = $diffdt->format("%R%a");
<?php if ( $differencedt >= 0 ) { ?>
<iframe src="<?php echo $video_url; ?>" frameborder="0" allow="autoplay; fullscreen" allowfullscreen=""></iframe>
<?php } else { ?>
<iframe src="<?php echo $teaser_url; ?>" frameborder="0" allow="autoplay; fullscreen" allowfullscreen=""></iframe>
<?php } ?>
The DateTime is what’s breaking the site. I’m sure I’m not using it correctly.
Thanks for any guidance!
There are two things that could be causing the page to crash. Before you go any further I would suggest that you turn on debugging so that you can see errors rather than just getting the WSOD https://codex.wordpress.org/WP_DEBUG.
Your first problem is that DateTime is a class so you need to use
$publish_date_time = new DateTime( get_sub_field('publish_date_time') );
more than likely you’re getting a fatal error that the function DateTime() does not exist.
After that you will need to make sure that your field is returning a date format that is compatible with DateTime() https://www.php.net/manual/en/datetime.formats.php.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.