Skip Navigation

[Resolved] How to use CRED date fields as timestamps in the API

This thread is resolved. Here is a description of the problem and solution.

Problem:
Client is trying to access the date fields submitted with a CRED form in a CRED hook, but the values available in $_POST are not in the timestamp format used to store the dates in the database.

Solution:
With the cred_save_data hook the post—and it's meta data—have already been saved, so using the post id it is possible to directly retrieve the date fields using get_post_meta that are already in the desired timestamp format.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by maksimG 6 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#602532

Hello
I have Types custom field wpcf-datab (whichstands for Data Begin)
Now I try to use following code in CRED API hooks

        $start_date_time = Date("Y-m-d H:i:s", $_POST['wpcf-dateb']['datepicker']);
	$start_date = date("Y-m-d", $_POST['wpcf-dateb']['datepicker']);
	$start_time = date("H:i:s", $_POST['wpcf-dateb']['datepicker']);

and as a result I got

2018-01-02 00:00:00
2018-01-02
00:00:00

but if I using

[types field='dateb' style='text' format='Y-m-d H:i:s'][/types]

I got

2018-01-02 10:30:00

as it should be

Help

Thank you

#602593

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

When using a date field for both date and time the value submitted with the form that you can retrieve from the $_POST object is for the date only (i.e. the time is midnight). The time is submitted as separate fields, and then the correct date&time timestamp calculated and stored in wp_postmeta.

Here is a sample of the $_POST object for a wpcf-begins field.

[wpcf-begins] => Array
        (
            [value] => Array
                (
                    [display-only] => January 1, 2018
                    [datepicker] => 1514764800
                    [hour] => 9
                    [minute] => 15
                )

            [name] => wpcf-begins
            [type] => date
            [repetitive] => 0
            [plugin_type] => types
        )

So, in short, you cannot retrieve the correct timestamp directly from the $_POST object.

However, if you are using the cred_save_data hook the post and postmeta have already been published by this point, and so you can simply retrieve the post meta value with the correct timestamp using something like the following:

$timestamp = get_post_meta( $post_id, 'wpcf-dateb', true );
#602851

Thank you !

P.S. Is there any place, where I could reach details of implementation (like you mentioned above) - It could save a lot of time for me and Tollset support.
Ive made several searches trough documentation and FAQ parts of the site - but fail
Thank you

Maxim