Skip Navigation

[Resolved] How to set a hidden date + time stamp on a form

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

Problem:
We would like to capture today’s date & time of when a user sets the custom field 'issue status' to complete, in a Add post CRED form.

Solution:
Add the following code in your theme’s functions.php file:

add_shortcode('wpv-today-day', 'today_shortcode');
function today_shortcode($atts) {
    return  current_time( 'timestamp', 0 );
}

Shortcode will be used like:

 [cred_field field='date-completed' post='maintenance-issue' value='[wpv-post-today]' urlparam='' class='form-control' format='d M Y h:m:s' output='bootstrap'] 
This support ticket is created 7 years, 4 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
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 11 replies, has 2 voices.

Last updated by Noman 7 years, 4 months ago.

Assisted by: Noman.

Author
Posts
#546986
Date Completed Field on CRED form.JPG
Date Completed Field.JPG

Hi,

I have a CRED form that is being used to update our 'maintenance issues' custom posts.

We would like to capture a date & time of when a user sets the custom field 'issue status' to complete.

What I've done so far is:
- create a custom date field called 'date completed'
- set a Conditional display rule that only shows if Issue Status = Completed ... screen shot attached
- on my CRED form I've added the 'completed date' field ... screen shot attached

The shortcode it's added is:

 [cred_field field='date-completed' post='maintenance-issue' urlparam='' class='form-control'  output='bootstrap']

Does this all look correct so far?

The next thing I'm not sure how to do is set the default value of today's date & current time in the shortcode ... what would it be?

I found some code for the function.php page from this post: https://toolset.com/forums/topic/date-format-in-a-cred-form-with-wpv-post-today/

So I have tried this code:

 [cred_field field='date-completed' post='maintenance-issue' value='[wpv-post-today]' urlparam='' class='form-control' format='d M Y h:m:s' output='bootstrap']

But it's not populating the field

Am I doing something wrong?

#547044

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Register_Custom_Shortcode.png

Hi Nick,

Thank you for contacting Toolset Support. The steps you have done looks correct. Can you please make sure to Register the "wpv-post-today'’ shortcode first in Toolset >> Settings >> Front-end Content >> Third-party shortcode arguments (see attached screenshot: Register custom shortcode.png)

And see if it works then?

Thank you

#547121
2. Test - code output.JPG
1. Test - code in Cred form.JPG
Shortcode-Registered.JPG

Hi Noman,

Thanks for your message. I've now registered the shortcode as per screenshot attached.

If I just put the shortcode on it's own it seems to work. Here is a test:
1. Screenshot shows the code in the CRED with the field and the shortcode on it's own
2. Screenshot shows the output ... you can see the field is empty by the shortcode and outputted the date & time ok

Mmmm ... not sure what's going on.

#547286

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello,

Please use this updated function and remove the older one you have added:

add_shortcode('wpv-today-day', 'today_shortcode');
function today_shortcode($atts) {
	return  current_time( 'timestamp', 0 );
}

Thank you

#547358
Date format.JPG

Hi Noman,

That's working now and I've managed to get it to work how we need it so that's great thank you.

But, when we export the data I notice that the format of the custom date fields are incorrect ... maybe in the time stamp format. Ideally we need it exporting in the date format like the default date modified field is in the attached screenshot.

In the screenshot you can see the custom date field 'Issue Date' coming out as '1487852248' instead of '2017-07-08'

Is this a setting in the WP Types custom field?

Thanks

#547387

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello,

Toolset use Unix timestamp to store values for date field type into database. So whatever date value as a timestamp stored in database you will not get that as export. As mentioned by Minesh here:
https://toolset.com/forums/topic/csv-export-of-date-fields-problem/#post-374610

Have a great day, Thank you

#547472

Hi Noman,

Oh dear ... that's quite a limitation for what we need.

If there is no way of fixing this, through php or js, is a possible workaround to use a single line text field and have the time stamp capture here in stead?

#547513

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Yes unfortunately there is no way to change it with php or js, may be through some in-depth coding will force it but not quite sure.

Using single line text field will work good as a workaround, in that case you will use old shortcode, 2nd one that was here:
https://toolset.com/forums/topic/date-format-in-a-cred-form-with-wpv-post-today/#post-211808

Thank you

#547786

Hi,

Final check ... I would like to use both the date field and the text field so need both codes in the functions.php file.

I've tried the single shortcode for both but the one working for date doesn't seem to work for the text field. So I also tried both like this:

 
// used to populate text field
add_shortcode('wpv-post-today', 'today_shortcode');
function today_shortcode() {
  return date("d M Y h:m:s", time());
}

// used to populate date field
add_shortcode('wpv-today-day', 'today_shortcode');
function today_shortcode($atts) {
  return current_time( 'timestamp', 0 );
}

But it crashes the site. Would you expect it to crash the site if I put both in?

THanks

#547824

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Yes, I am expecting same. Because we can’t use two functions with same name. Now I have updated function name and it will not generate any issue.

// used to populate text field
add_shortcode('wpv-post-today', 'today_shortcode');
function today_shortcode() {
  return date("d M Y h:m:s", time());
}
 
// used to populate date field
add_shortcode('wpv-today-day', 'today_day_shortcode');
function today_day_shortcode($atts) {
  return current_time( 'timestamp', 0 );
}

Thank you

#547832

Works great ... thank you so much for all your help!!

#547857

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Great to hear that it all works fine now 🙂 Have a wonderful day. Thank you