Support

Account

Home Forums General Issues DatePicker returns object if no date set

Solving

DatePicker returns object if no date set

  • I’m having a problem with PHP errors for dates. I have a field so that once a user is emailed a notice about their membership, the date gets stored so they don’t get emailed again. Initially this field is empty but I’m getting a lot of errors like this:

    Warning: strtotime() expects parameter 1 to be string, object given in /var/www/vhosts/domain.com/httpdocs/wp-content/plugins/advanced-custom-fields-pro/fields/date_picker.php on line 217 Warning: DateTime::createFromFormat() expects parameter 2 to be string, object given in /var/www/vhosts/domain.com/httpdocs/wp-content/themes/ddi-v1/functions-members.php on line 301

    Line 301 has this:

    $status->renewal_email_date = DateTime::createFromFormat('Ymd', get_field('field_557ff26b07d38', 'user_' . $user_id));
    

    It seems that if no value is stored for the date field, an empty object is returned and this causes an error for the DateTime class. I’ve tried some code to check for an object but it still fails:

    (!is_object(get_field('field_557ff26b07d38', 'user_' . $instructor->ID))) ? the_field('field_557ff26b07d38', 'user_' . $instructor->ID) : 'No date!';

    Error:

    Warning: strtotime() expects parameter 1 to be string, object given in /var/www/vhosts/domain.com/httpdocs/wp-content/plugins/advanced-custom-fields-pro/fields/date_picker.php on line 217 
    

    Is this the correct behaviour?

    Thanks

  • Hi @davemccourt

    Hm. Any ACF field where no one has added any values (manually or programmatically) should just return false since they don’t even exist in the DB yet. Are you sure you haven’t got any code creating an empty value or maybe you’ve added and then deleted the date?

    As for your check maybe something like this works? I assume you’re in a function that would send out an email if it keeps going.

    
    <?php
    if( !isset(get_field('field_557ff26b07d38', 'user_' . $instructor->ID)) ){
    	return false;
    ?>
    
  • Thanks @jonathan

    I think the issue is I was calling get_field with the field key instead of the field name. It seems that ACF returns an empty object for this if there is no value. When I switched to the field name, I stopped getting errors.

  • Ah yeah that could also be it 🙂

    Glad you worked it out!

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

The topic ‘DatePicker returns object if no date set’ is closed to new replies.