Skip Navigation

[Resolved] The date field is displaying in UTC but saving correctly.

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

Supporter timezone: Europe/Madrid (GMT+02:00)

This topic contains 8 replies, has 3 voices.

Last updated by rosemaryW 7 years, 7 months ago.

Assisted by: Juan.

Author
Posts
#422279

I am trying to: Get the custom date in a view to display correctly.

I visited this URL: The site is not public, yet. I can provide credentials outside of a public forum.

I expected to see: Posts from a loop displaying from the current_month

Instead, I got: They are all outputting UTC

#422363

Hello,
For date formatting you can use parameter “format” with any valid WordPress date value inside.

Please check our Docs about Toolset Date Field here: https://toolset.com/documentation/customizing-sites-using-php/functions/#date
And about WordPress date formats here: https://codex.wordpress.org/Formatting_Date_and_Time

Quick example with custom date format:

[types field="my-date" format="Y/m/d g:i:s A"][/types]

This shortcode will display the date in the format 2016/08/04 8:00:00 AM
Don’t forget to change “my-date” with your field slug
Also if you want to output your date in different timezine from UTC, you need to use timezone offset functions in combination with hook

add_filter('wpcf_fields_slug_my-date_value_get', 'default_value_func', 10, 2);
function default_value_func($meta, $this){
    if(empty($meta)){
    $tz = new DateTimeZone('America/Los_Angeles');  // YOUR TIMEZONE HERE
     $date = new DateTime($meta');
     $meta = $date->setTimezone($tz);
    }
    return $meta;
}

The same thing here, don’t forget to change “my-date” with your field slug

Another option, you can set default timezone to your website via theme functions.php:

function op_fix_timezone() {
    date_default_timezone_set('America/New_York');
}
add_action('init', 'op_fix_timezone', 1);
#429852

Aloha Serhii,

Your solution has the post displaying and saving the right date, but the filter for the loops are still not working.

I am trying to filter posts from our custom date field to display "THIS MONTH".
I can get it to display correctly for previous months is using the filter:
"
Select items with field:
Hot Pick Date is a number between PAST_MONTH(2), PAST_MONTH(1)"

But no combination of filters get the current month to display.

Thank you,
Nathan

#430838

Hello Nathan,

The code you're using will return posts from June 1st at 00:00 to July 1st at 00:00

Please try this one to get posts with a date field with a value in the current month:

between THIS_MONTH(), FUTURE_MONTH(1)

which will get date field values from August 1st to September 1st

#431007
loop-problem.jpg

Aloha Serhii,

Is there a way to send you access to our test site?
I am still having difficulty getting posts to display correctly in my loops.

Enclosed is a screen shot of the problem.

Thank you,
Nathan

#431739

Juan
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Hi Nathan

This is Juan, lead Views developer here. I see that you have a problem filtering by a date field. We already know they are a little complex to understand, and we are thinking about improving them a little. But on the meantime, I think we can try to solve your issue first.

Let me start with some background. When filtering by a date field, you can use a series of functions:
- NOW means the moment where the filter is executed, meaning that same day, month and year, but also hour, minute and second. It is good to get items that happen before and after the current moment when they get queried.
- TODAY means on this same day, month and year, but on the exact 00:00 am hour and minutes. It is good for when you need to list for example items that are due for today and later, for example.
- FUTURE_DAY(X) and PAST_DAY(X) need a parameter, which sets the value. For example, PAST_DAY(1) means yesterday at 00:00 am. Note that it does not mean yesterday during the whole day, those functions are not intervals but moments, and the moment is set by the day plus always 00:00 am. FUTURE_DAY(1) means tomorrow at 00:00 am.
- THIS_MONTH, FUTURE_MONTH(X) and PAST_MONTH(X) work basically the same: THIS_MONTH s the exact moment of the first day of this current month at 00:0 am. FUTURE_MONTH(2) means November 1st, 2016 at 00:00 am.
- THIS_YEAR, FUTURE_YEAR(X) and PAST_YEAR(X) work the same. THIS_YEAR means January 1st, 2016 at 00:00 am. FUTURE_YEAR(3) means January 1st, 2019 at 00:00 am.

There are other functions that are not usefull for us here.

So, if you want to grab posts from last month, as you stated, you set the flter to be "between" those two values: PAST_MONTH(1), THIS_MONTH, which means posts with date field values from August 1st, 2016, 00:0 am (PAST_MONTH(1)) to September 1st, 2016, 00:00 am (THIS_MONTH).

Now, you want to grab posts for the current month, so you need to start from September 1st, 2016, 00:00 am and close at October 1st, 2016, 00:00 am. The filter would be "between" and then as values THIS_MONTH (meaning September 1st, 2016, 00:00 am), FUTURE_MONTH(1) (meaning 1st October, 2016, 00:00 am).

In your screenshot, you are not using those values. Mind to give it a try and let me know how it goes?

Regards.

#433867

Aloha Juan,

Thank you for all the help. Wp-types has made development of complex sites a real pleasure.

I am still having troubles with the filters.

The filter for "This Month" is:
Hot Pick Date is a number between THIS_MONTH(), FUTURE_MONTH(1)

It displays posts from September 2 to October 1.

Is there a way to have the filter only check the "Month" and not a time stamp? As I see the problem, "THIS_MONTH" should be checking against the custom date month as it appears in the admin. I think the custom date gets stored in the db as a time stamp and the query does not incorporate the time zone conversion.

Thank you, again,
Nathan

#434021

Juan
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Hi Nathan

I am sorry, I am not following your request. You wanted a way to get posts with a date field that matches the current month, and that is tht a filter between THIS_MONTH() and FUTURE_MONTH(1) do: it gets posts from September 1st, 2016, 0:00 am, to Octuber 1st, 2016 00:00 am, which is exactly the efinition of "posts with a date field value in this month".

Date fields are stored as timestamps, so to compare their values we need to manage as they are: timestamps. I guess that what you are suggestiong is that a filter with value THIS_MONTH() should check for timestaps which converted value to a "month" date matches the current month value, but that can not be done for two main reasons:
* First, the database interaction does not work that way. It would require that we convert all the timestamps stored in date fields to date valus on-the-fly, and then compare against th desired value. That is simply not doable.
* Second, backwards compatibility is important. There are several sites relying on the syntax and meanings we have in place now. Even if I do agree that the date functions we use seem weird and are aplied in not the best way, we can not simply change what they mean overnight. We can extend then to new values, like a filter that simply gets posts with a date field in DURING_THIS_MONTH(), that will then transofrm itself into the "between" filter that we need to add manually now, and maybe also renew the labels of the existing filters to, say, STARTING_THIS_MONTH() instead of just THIS_MONTH(). But one thing is extending and updating labels, and another is changing how something actually works.

in fact, I am noting down this last suggestion as a feature request. Let's see whether we can dedicate some time to it soon.

Hope it helps.

Regards.

#435783

Mahalo!

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