Skip Navigation

[Resolved] How to automatically set a date field to the current date/time

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

Problem:
A Toolset Form is used to publish posts. A custom date field should store the time the form was submitted.

Solution:
You would use the cred_save_data hook to trigger code that sets a custom field value with the current timestamp using update_post_meta.

An example (which updates two custom fields) is given below: https://toolset.com/forums/topic/automatic-filling-date-fields/#post-1251117

Note that a simpler implementation would be to just use the standard post_date field of the post published with the form.

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

100% of people find this useful.

This support ticket is created 5 years, 6 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 36 replies, has 3 voices.

Last updated by stephaneB-5 5 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#1255177

it's written "le site rencontre des probleme"
but no php error

#1255179

add_action('cred_save_data', 'tssupp_form_submit', 10, 2);
function tssupp_form_submit($post_id, $form_data)
{
// Edit
$start_form_id = 83; // ID of form with start time
$end_form_id = 130; // ID of form with end time
$start = 'date-d-entree'; // slug of start field
$end = 'date-de-sortie'; // slug of end field

// Types date field expects date to be stored as "local" timestamp
$site_timezone = get_option('Europe/Paris');
$timezone_offset_in_seconds = timezone_offset_get(timezone_open($site_timezone), new DateTime());
$now = time() + $timezone_offset_in_seconds;

if ($form_data['id'] == $start_form_id) {
update_post_meta($post_id, 'wpcf-' . $start, $now);
} elseif ($form_data['id'] == $end_form_id) {
update_post_meta($post_id, 'wpcf-' . $end, $now);
}
}

#1256925

Hi Stephane,

Thanks for writing back and for sharing the code.

Nigel is on vacation and will be back tomorrow and I'll be following up, while he is away.

I noticed that you've replaced line# 15 from Nigel's code snippet from:
( ref: https://toolset.com/forums/topic/automatic-filling-date-fields/page/2/#post-1252795 )


$site_timezone = get_option('timezone_string');

To:


$site_timezone = get_option('Europe/Paris');

This isn't correct and you can revert that line back to how Nigel shared it.

What he meant was that you should go to your website's WP Admin -> Settings -> General and make sure that "Paris" is selected in the "Timezone" option.

Note: In case the website still shows "le site rencontre des problemetitle" error, after reverting to the Nigel's original code, please enable WordPress debugging on ( ref: https://wordpress.org/support/article/debugging-in-wordpress/ ) and let us know if there any errors/warnings shown on the screen or in server's error log.

regards,
Waqar

#1258069
Capture d’écran 2019-06-04 à 12.27.32.png
Capture d’écran 2019-06-04 à 12.27.22.png

thkx Waqar,
it works:-) but when I sort elements by date It's not working.
I don't understand why it's doesn't work.
Can you help me ?

New threads created by Nigel and linked to this one are listed below:

https://toolset.com/forums/topic/ordering-with-date-fields/

#1260749

I test every option but it doesn't work 🙁

#1260863

Nigel
Supporter

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

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

Hi Stephane

Waqar is off and I've taken back this ticket.

It looks like the original issue is resolved, and as this thread has become very long I've split the follow-up question into a new thread and I'll answer there.

To make the forums searchable by other users with related issues we try to limit the threads to a single issue.

#1260895

My issue is resolved now. Thank you!