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.
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"]
My issue is resolved now. Thank you!