Skip Navigation

[Resolved] Display time using shortcode after adjusting based on time zone stored as CF

This support ticket is created 5 years, 8 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)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Ben 5 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#1220079

Ben

I am able to display the current time for any time zone using the code below.

add_shortcode('shortcode-time', 'pts');
function pts() {
date_default_timezone_set("Asia/Beirut");
return date( 'H:i', current_time( 'timestamp' ));
}

This works fine but the timezone has to be predetermined as it is set within the function. I would like the ability for the time displayed to be determined by a custom field on the post where the shortcode is displayed. This custom field will indicate which time zone to display.

I have created a custom field with the meta key "wpcf-timezone-select-test". In this field various timezones are stored in the appropriate formats (Continent/City). These are then set depending on what time is required to be set.

I am trying the below to show this but it doesn't seem to be working.

add_shortcode('shortcode-time', 'pts');
function pts() {
date_default_timezone_set( get_post_meta(get_the_ID(), 'wpcf-timezone-select-test'));
return date( 'H:i', current_time( 'timestamp' ));
}

Can you see the reason why this is failing?

#1220259

Nigel
Supporter

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

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

Hi Ben

get_post_meta returns an array, not a string, unless you add a third parameter of 'true', I think that would be your problem.

https://developer.wordpress.org/reference/functions/get_post_meta/

#1220828

Ben

Hello Nigel,

That was it! Thank you so much for helping.