Skip Navigation

[Resolved] Relationship form with empty date fields results in 1970-01-01 being shown

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by amandaD 1 year, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#2603301
Screenshot 2023-05-05 140413.png

I am trying to:
I have a CRED form to edit/create a relationship. The relationship has several date/time fields, and these are shown in my CRED relationship form as so:
[cred-relationship-field name='coaching-session-datetime-30']

When these fields are displayed on the single CPT view, they are diplayed like so: [types field='coaching-session-datetime-90' style='text' format='Y–m–d h:i a'][/types], wrapped in a conditional that checks if the date not empty, and if the date is before TODAY()

Link to a page where the issue can be seen: hidden link (the coaching tab)

I expected to see:
The relationship date fields are optional. When I submit the relationship form with some empty date fields, I expect to see blank/empty date fields.

Instead, I got: 1970-01-01 12:00 am (the linux epoch).

Second issue - possibly related? When I add/edit the relationship on the WordPress backend, my dates are displayed on the frontend as: `2023–05–31 09:00 am`. This is correct.

If I use the frontend relationship form to add/edit the relationship, my dates are displayed as `2023–05–09 12:00 am 2023–05–09 12:00 am 1970–01–01 12:00 am` This is incorrect.

My relationship form looks like this:

```
[wpv-noautop]
[cred-relationship-form-container]

<!-- TRAINEE -->
<div class='row mb-4'>
<div class='col-12 col-md-3 col-lg-2 text-md-right text-md-end'>
<label class='form-label fw-bold m-0 align-items-center' for='%%FORM_ID%%_child'>
[cred_i18n name='child-label']Trainee[/cred_i18n]
</label>
</div>
<div class='col-12 col-md-9 col-lg-10'>
[cred-relationship-role role='child' orderby='title' order='ASC' class='form-select' output='bootstrap']
</div>
</div>

<!-- NAME -->
<div class='row mb-4'>
<div class='col-12 col-md-3 col-lg-2 text-md-right text-md-end'>
<label class='form-label fw-bold m-0 align-items-center' for='%%FORM_ID%%_parent'>
[cred_i18n name='parent-label']Coach[/cred_i18n]
</label>
</div>
<div class='col-12 col-md-9 col-lg-10'>
[cred-relationship-role role='parent' orderby='title' order='ASC' class='form-select' output='bootstrap']
</div>
</div>

<div class='row mb-4'>
<div class='col-12 col-md-3 col-lg-2 text-md-right text-md-end'>
<label class='form-label fw-bold m-0 align-items-center' for='%%FORM_ID%%_coaching-session-datetime-30'>
[cred_i18n name='coaching-session-datetime-30-label']30 day[/cred_i18n]
</label>
</div>
<div class='col-12 col-md-9 col-lg-10'>
[cred-relationship-field name='coaching-session-datetime-30']
</div>
</div>

<div class='row mb-4'>
<div class='col-12 col-md-3 col-lg-2 text-md-right text-md-end'>
<label class='form-label fw-bold m-0 align-items-center' for='%%FORM_ID%%_coaching-session-datetime-45'>
[cred_i18n name='coaching-session-datetime-45-label']45 day[/cred_i18n]
</label>
</div>
<div class='col-12 col-md-9 col-lg-10'>
[cred-relationship-field name='coaching-session-datetime-45']
</div>
</div>
<div class='row mb-4'>
<div class='col-12 col-md-3 col-lg-2 text-md-right text-md-end'>
<label class='form-label fw-bold m-0 align-items-center' for='%%FORM_ID%%_coaching-session-datetime-60'>
[cred_i18n name='coaching-session-datetime-60-label']60 day[/cred_i18n]
</label>
</div>
<div class='col-12 col-md-9 col-lg-10'>
[cred-relationship-field name='coaching-session-datetime-60']
</div>
</div>
<div class='row mb-4'>
<div class='col-12 col-md-3 col-lg-2 text-md-right text-md-end'>
<label class='form-label fw-bold m-0 align-items-center' for='%%FORM_ID%%_coaching-session-datetime-90'>
[cred_i18n name='coaching-session-datetime-90-label']90 day[/cred_i18n]
</label>
</div>
<div class='col-12 col-md-9 col-lg-10'>
[cred-relationship-field name='coaching-session-datetime-90']
</div>
</div>
<div class='row mb-4'>
<div class='col-12 col-md-3 col-lg-2 text-md-right text-md-end'>
<label class='form-label fw-bold m-0 align-items-center' for='%%FORM_ID%%_coaching-session-datetime-135'>
[cred_i18n name='coaching-session-datetime-135-label']135 day[/cred_i18n]
</label>
</div>
<div class='col-12 col-md-9 col-lg-10'>
[cred-relationship-field name='coaching-session-datetime-135']
</div>
</div>

<div class="row">
<div class="col-12 text-center">
[cred-form-submit field='submit' name='submit' class='btn btn-trainee']
[cred-form-cancel field='cancel' name='cancel' class='btn btn-light']
</div>
</div>
[/cred-relationship-form-container]
[/wpv-noautop]
```

#2603907

Hello,

I have tried it in a fresh WP installation + the latest version of Toolset plugins, it works fine.

See below sandbox website:
Login URL: hidden link

1) Relationship form with custom date field:
hidden link

2) Test it in frontend:
hidden link
Fill and submit above relationship form

3) Check the new intermediatory post in admin side:
hidden link

I can see the custom date field value is empty, same as in frontend:
hidden link

Is there any missing steps? Can you reproduce the same problem in above sandbox website? thanks

#2603959
screenshot-2.png
screenshot 1.png

Hello. Yes, I can reproduce the problem when I change the relationship form to require both date AND time, then I get the same 1970 problem. See the intermediary post here: Pages Posts: 24 - 26

I edited the post field group for the relationship to require both date and time (screenshot 1). When I make this change, I can reproduce the error (screenshot 2) It works fine if there is only the date.

#2604289

Thanks for the detail, you are right, the datepicker of custom date field does not pass value to relationship form, but the time and second fields do pass values.

As a workaround, you can use custom codes to remove custom date field value if the date field is empty, for example:

1) Add a custom code "remove-empty-date":

add_action( 'added_post_meta', function( $mid, $object_id, $meta_key, $_meta_value ) {
  
    if( get_post_type($object_id) == 'page-post' 
       && isset($_POST['wpcf']['coaching-session-datetime-30']['display-only'])
       && empty($_POST['wpcf']['coaching-session-datetime-30']['display-only'])
    ){
      	delete_post_meta($object_id, 'wpcf-coaching-session-datetime-30');
      	wp_cache_delete( $object_id, 'post_meta' );
    }
}, 99, 4 );

Please replace "page-post" with the many-to-many relationship slug
replace "coaching-session-datetime-30" with your custom date field slug

2) Test it in frontend again:
hidden link

It works fine after create new relationships.

More helps:
https://developer.wordpress.org/reference/hooks/added_meta_type_meta/

#2604315

My issue is resolved now. Thank you!