Support

Account

Home Forums General Issues datepicker return today's day if field empty

Solving

datepicker return today's day if field empty

  • hi. i’ve seen this reported if strtotime is used, which i do. i have a ‘single_event_date_end’ datepicker field which returns today’s (current) date if i leave the field empty, and i’d prefer it to return nothing, but i’m struggling to write a function that achieves this.

    the code i’ve tried is (variations on):

    function event_date_end()
    {
        $event_date_end = get_field("single_event_date_end"); 
    
        if ($event_date_end == null) {
            return false; 
        } else {
            return $event_date_end;     }
    }

    any advice appreciated. hiding it isn’t the problem; i’d just like null to = null.
    thanks in advance!

  • ACF may return false or NULL for fields. Generally it returns false when a field has no value (empty string saved in DB) and NULL if the value was never saved to the DB.

    The best bet for testing either of these is

    
    if (empty($event_date_end))
    
  • hi John. the problem, i’ve learned, is that ACF returns the current date if using strtotime and date_i18n. i’ve tested running a plain vanilla event_date_end and there’s nothing there, but as also reported here, this isn’t always the case!

    if i use your IF statement, is the rest of it basically correct?

  • I’m not exactly sure of the issue, but I do know that any call to a php date related functions with empty values for the date (null, false, empty strings, anything that PHP considers “empty”) result in PHP using the current date time. The only way around this it to make sure that the value is not empty before you apply php date functions to that value.

  • this field is for an ‘events’ CPT that i’ll be consistently adding to in the future, so that’s not really an option. thanks for taking the time to have a look at it, though, much appreciated!

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

You must be logged in to reply to this topic.