Skip Navigation

[Resolved] Wrong Time in Cred Field

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

Problem:
Client has created a custom shortcode to set the default value for a custom date field in a Toolset Form, but it is not allowing for the time zone.

Solution:
An updated shortcode to show the current date/time adjusted for the time zone that can be used to set the default value of a date field is:

add_shortcode( "now", function(){
 
    // get UTC offset for current WP timezone
    $offset = get_option('gmt_offset');
 
    return time() + $offset * 60 * 60;
});

Relevant Documentation:
https://toolset.com/documentation/adding-custom-code/

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

Last updated by Nashaat 6 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1108222
Correct-Time-in-Backend.png
Wrong-Time-from-Shortcode.png

I am trying to get the current time and date in a cred custom field.

I have created a custom field called "general-date-field" and added this to Cred form.

now i have added a code snippet to get the current time and date with following code

add_action('cred_save_data_3357', 'update_date_last_edited', 10, 2);
function last_edited() {
   return time();
}
add_shortcode('wpv-post-edited-time', 'last_edited');

then i have added the shortcode of that snippet to the fields Value like following

<code
[cred_field field='general-date-field' value='[wpv-post-edited-time]' urlparam='' class='form-control' output='bootstrap']
[/php]

now the default value of that field is showing up but always UTC+0...Although my WordPress site time is UTC+3 and in the backend admin it shows me the right time which is UTC+3

#1108287

Nigel
Supporter

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

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

Hi there

I know why this happens—it is a problem with how we store date fields—but I want to get some feedback from the Types developers about whether this might change before I reply to you.

That won't be until Monday now, so I'll reply to you then, I hope that's okay.

#1108288

Yes Sure Monday is ok. nice weekend

#1109439

Nigel
Supporter

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

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

You should modify the code in your shortcode to generate the current time so that it adds in the timezone offset, because time() generates the current UTC time, and you want the local time.

This code registers a now shortcode which will return the current time adjusted for your timezone settings:

add_shortcode( "now", function(){

	// get UTC offset for current WP timezone
    $offset = get_option('gmt_offset');

	return time() + $offset * 60 * 60;
});

You would then use it in the value attribute of your cred_field shortcode.

#1109509

Thats Awesome! it works now yes. Thank you Nigel