Support

Account

Home Forums General Issues Date Time Picker Check

Solving

Date Time Picker Check

  • I want to display a certain callout on my site (it is an ACF image field), but I want it only to display during a certain time.

    I am looking to use a Date Time Picker field for the start date/time and a Date Time Picker for the end date/time. However, how would I check to see if the current date/time is within the start and end time frame?

  • this is only one possibility

    
    // current unix time stamp
    $now = time();
    // get field with no formatting
    // this will return date time as stored in the db
    // to that there isn't any problem with the date format
    // when calling strtotime()
    $start = strtotime(get_field('start_date_time', false, false));
    // same for end
    $end = strtotime(get_field('end_date_time', false, false));
    if ($start <= $now && $now <= $end) {
      // in the middle, do something.
    }
    
  • Awesome.

    And since I am actually using this in conjunction with a javascript function, would the below work?

    <script>
    var nowTime = <?php time(); ?>;
    var startTime = <?php strtotime(get_field('start_date_time', false, false)); ?>;
    var endTime = <?php strtotime(get_field('end_date_time', false, false)); ?>;
    if (startTime <= nowTime && nowTime <= endTime) {
    //do something
    }
    </script>

    Also, I am assuming the PHP time(); function and the Date Time Picker both use the server’s time zone, correct?

  • You would need to echo the values to get them into the JS

    
    <script>
    var nowTime = <?php echo time(); ?>;
    var startTime = <?php echo strtotime(get_field('start_date_time', false, false)); ?>;
    var endTime = <?php echo strtotime(get_field('end_date_time', false, false)); ?>;
    if (startTime <= nowTime && nowTime <= endTime) {
    //do something
    }
    </script>
    
  • Oh yeah, of course. Missed that.

    Thanks!

  • And I am assuming the time() function uses the server’s time zone, correct? So the Date Time Picker date/time selected should use that same time zone as the server, correct?

  • Yes it does, but I don’t know how ACF gets the current time, I don’t really think it needs to get the time because you use fields to set times. There is a function in wp to get the time https://codex.wordpress.org/Function_Reference/current_time, but in the end it’s just a complicated way to call time().

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

The topic ‘Date Time Picker Check’ is closed to new replies.