Support

Account

Home Forums ACF PRO Date Time with GMT Offset Difference

Helping

Date Time with GMT Offset Difference

  • 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.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Date Time with GMT Offset Difference’ is closed to new replies.