Support

Account

Home Forums Front-end Issues Show content based on timepicker value

Solving

Show content based on timepicker value

  • Could you really do with some help. I have created this so far….

    <?php
        date_default_timezone_set('Europe/London'); // timezone 
        $weekday = date(l); // today
        $now = new DateTime();
        $morning = $now >= new DateTime('13:30') && $now <= new DateTime('13:40');
        $afternoon = $now >= new DateTime('15:20') && $now <= new DateTime('15:45');
        if($weekday == "Friday" && ($morning || $afternoon)) : ?> 
        	<h2>We're streaming</h2>
         
         <?php else : ?> 
         	<h2>No streaming</h2>
     
    <?php endif; ?>

    But I want to use the acf timepicker option instead of hardcoded time (the day is fine as is). I’ve tried to work it out but can’t seem to implement the acf fields instead of the time.

    <?php the_field(‘morning_start_time’); ?>
    <?php the_field(‘morning_stop_time’); ?>
    <?php the_field(‘evening_start_time’); ?>
    <?php the_field(‘evening_stop_time’); ?>

    Any thoughts on this would be appreciated

  • You need to use get_field() to return the value. the_field() echos/outputs the value.

  • Thanks so much

    So would that be

    $morning = $now >= new DateTime(‘<?php get_field(‘morning_start_time’); ?>’)

  • I could be slow, but I don’t understand the syntax

    
    $morning = $now >= new DateTime('13:30') && $now <= new DateTime('13:40');
    

    What exactly is this supposed to be doing and where can I find information on this syntax

    
    $x = $y >= $a && $y <= $b
    
  • So its setup so that content:
    <h2>We’re streaming</h2>

    Will show between the times –
    $morning = $now >= new DateTime(’13:30′) && $now <= new DateTime(’13:40′);
    $afternoon = $now >= new DateTime(’15:20′) && $now <= new DateTime(’15:45′);

    Any other time it will show:
    <h2>No streaming</h2>

  • Yes, then your code would be correct
    $morning = $now >= new DateTime('<?php get_field(‘morning_start_time’); ?>')
    however, this depends on the ACF field returning a the value format that is compatible with PHP date/time functions.

  • Thanks for looking at this

    <?php get_field(‘morning_start_time’); ?>

    Was throwing up an error, because of php within php maybe? Went with

    <?php
        date_default_timezone_set('Europe/London'); // timezone 
        $weekday = date(l); // today
        $now = new DateTime();
        $morning = $now >= new DateTime(get_field('morning_start_time','option')) && $now <= new DateTime(get_field('morning_end_time','option'));
        $afternoon = $now >= new DateTime(get_field('evening_start_time','option')) && $now <= new DateTime(get_field('evening_end_time','option'));
        if($weekday == "Saturday" && ($morning || $afternoon)) : ?> 
        	<h2>We're streaming</h2>
         
         <?php else : ?> 
         	<h2>No streaming</h2>
     
    <?php endif; ?>

    It doesn’t throw up an error but its also not working. Would that be because of the php date/time functions? My return format is H:i currently

    Or is there an easier way to make this work? I just can’t work this out

  • The times I’m trying to show are acf fields on an options page. Would that make a difference as to it working or not here?

  • Yes, the reason it is not working is that you are not returning a date/time value that is compatible with PHP date functions.

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

You must be logged in to reply to this topic.

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.