Skip Navigation

[Resolved] How to output Time as UTC in custom date field

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

Problem:

Display the custom date field value with different timezone.

Solution:

You can try custom codes, for example:

https://toolset.com/forums/topic/how-to-output-time-as-utc-in-custom-date-field/#post-1606637

Relevant Documentation:

This support ticket is created 3 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by toddU 3 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#1605821

I am trying to output just the hours and minutes from a custom field so that I can use it for a hyperlink. Currently, I am using format='Hi' to output what I need. The problem is that it outputs on my timezone. I need the time to be UTC or Unix.

My website is setup on the New York timezone (UTC-4).

If I enter 16:00 for the time on my website, I will get 1600 when I use the 'Hi' format. But what I really want is 2000.

I believe the website is converting the 16:00 to a Unix timestamp based on my timezone selection and then storing that timestamp. Then when I retrieve it, it is converting it back to my timezone automatically. Am I right on this?

With all the changes to time within WordPress in the recent year or two, this is quite difficult to figure out what's what and how to make it work right. Any suggestions would be greatly appreciated.

#1606637

Hello,

I assume we are talking about a custom date-time field created with Toolset Types plugin.
The custom date-time field stores value in as timestamp format in GMT, for example 2020-04-30 16:00, the timestamp value is: 1588262400, when you output the custom date field value with Types shortcode, for example:

[types field='test-date' style='text' format='Hi'][/types]

It will output the date time value as what you input into the custom date-time field: 1600

In your case, you will need to consider custom codes, for example:
1) Add below PHP codes into your theme file "functions.php", creat a shortcode:

add_shortcode('my-date-field', function($atts, $content){
	$atts = shortcode_atts( array(
		'format' => 'Hi',
		'field'	=> 'test-date'
	), $atts);
	$ts = get_post_meta(get_the_ID(), 'wpcf-' . $atts['field'], true);
	$res = '';
	if($ts){
		$res = date_i18n($atts['format'], $ts - 3600 *( -4 ) ); // here setup the time zone -4
	}
	return $res;
});

2) Use above shortcode, like this:

[my-date-field field="test-date" format="Hi"]
#1607085

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.