Skip Navigation

[Resolved] how to filter view by working time?

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

Our next available supporter will start replying to tickets in about 2.16 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 7 replies, has 2 voices.

Last updated by Beda 6 years, 4 months ago.

Assisted by: Beda.

Author
Posts
#924375

I've got a CPT for offices, and they have working hours (2 fields: at work week and at weekend). I need to make a filter for currently open offices.

A kind of dumb way of doing it would be to add a checkbox (is opened) and run a cron say every few minutes and update this flag for every branch (there are few hundred).

But is there a way to do it without such checkbox using Views hooks? Some custom filter maybe, with custom query. The filter should look like a 'is opened now' checkbox.

Currently, working hours are defined as ranges: 08:00-22:00, but I can split them into start/stop.

#924624

Well, there are 2 parts of the deal here.

1. Filter correctly the data - even as raw filter may be a little tricky here
2. After, I would simply pass the "current time" of the visitor (With JS) to the Checkbox you speak of.

When the user selects it, your code passes the "Current time" simply to the URL parameter of the View's Query Filter which is added to the View since you filter it.

For example you can filter a Date field by UNSIGNED in a between value.
https://toolset.com/documentation/user-guides/date-filters/

This can be done for 2 Date fields as well.

If you craft a JS code that grasps the Current visitor time and populates the URL attribute generated by Views, you can do exactly what you need.

You could even filter the View with some values in PHP, but I guess it is nicer to have this in JS?
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

With that hook you can modify the query of the view, but you still need to pass it the current users time.

#924654

I don't even need user time, server time will be enough. All these offices are in the same country, and sever time will be local.

But even if I take user time, how does it help me to make the filter?

#924979

I think I am not grasping correctly what you want to achieve and depict to me as the issue, otherwise you would just use a date filter, but re-reading your report, I think I did not understand correctly.

I need more details.

1. How exactly do you store the information you want to filter by?
My understanding was a Date/Time Field but it seems if I re-read this, you use a single Field or ranges in a select?
Can you explain how you store, for the "CPT for offices", the data you want to filter by, and what format that data has?

2. I think, re-reading this, that you need a filter, which compares the values which are NOT in a timestamp or classic "time" format, to a classic time format or even exact UNIX timestamp (compare the 08:00-22:00 to NOW)

That should be possible.

But, it requires more code that is not using Toolset API at all, than actual Toolset API.

1. Getting the NOW time is easy, as you say, even easier since you just would have one location to get it from.
2. Then you need to compare the Field you have (Weekday, Weekend will be handled separately depending on above #1 result) with that NOW time.
3. This should be easy after you convert either of both formats of time to the same.
4. Then, you filter the View output, and only return posts where the Fields match above compared time values.

However you compare both values, and convert them is a question of PHP generic coding.
You can then file the Post that the View returns with https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
That filter allows you to modify the query.
So, if you have a value which you want to match (NOW time) you can simply filter those posts for all posts field fields matching the NOW value.
But, since those fields have a value that first needs to be converted to something that makes sense to the Machine (if I understand your setup right) then you need to convert that value ahead of using it.

For example you could fake a time.
Let's say you have stored 08:00-22:00
You can use that to fake a timestamp that is at a max and minimum of TODAY, that time.
Now you have some values to compare in your code with the NOW timestamp, and can filter the query by it.

But this requires complex custom code, which is not related to Toolset.
Our Support capabilities are restricted here:
https://toolset.com/toolset-support-policy/

I could help, if we can do this on a more abstract and slimmed down example that we can use for other users as well.

I can try to find a solution with minimal code involved.

I need clear specifications or confirmation of what I outline above.
I would, if I understand it right, use a Field with options to store those 08:00-22:00 values. That is how you have set it up, right?
There are 2 of them (weekday and weekend?)

Can you outline this to me, so I may come up with a complete sample for such a scenario or a different solution.

As well, I am aware of the requirement of recurring events in the date field of Toolset - something that is filed as request and would help here too.
But I have no ETA on that.

I am waiting for your specifications

#929144

Let's make it simpler with 4 fields:
working-days-open (08:00)
working-days-close (22:00)
friday-open (08:30)
friday-close (14:00)

So first of all, we'll have to choose whether to check the first pair or the second pair.

And then whether to append these times to TODAY and compare to NOW, or take the TIME(NOW) and compare to these fields directly (is there a function that strips the date part?).

#950045

OK, I understand the approach.

working-days-open (08:00)
working-days-close (22:00)
friday-open (08:30)
friday-close (14:00)

The search would then compare the current time, to the above time-frames.
Ideally that would happen with one click of a button or check of a box.

This is not possible with Toolset out of the box. It requires elaborated Custom Code that we cannot provide here.
You may contact the Consultants for this part:
https://toolset.com/contractors/

Here is what you could do with Toolset:
- Create the content types (Post and Fields)
- Populate it either in the backend or front end with Forms
- Create a View, that queries your posts.
- Add several Custom Filters for starting time, ending time, working days and weekends.

The most minimal possible setup are 4 Post Fields of type single line (Start and end of working days, same for Weekends)
The Single Line fields get populated with time values in the 24hrs format:
01:00 - 23:00
In a View Custom Filter you can add filters by those fields.
You may add in-between filter comparison so the values returned can be set according a range of values and not just one specific hour.

However, bringing all those into one filter is not possible without Custom Code, and that would only solve the Query part, not how you actually pass the chosen values to the Query (which you would need to do with some Custom HTML input, and then listen to that value when you submit the search/run the filter on the Views Query.)

We have features filed to allow recurring times in the Date Field and that will allow a large set of what's missing right now.

However, we cannot provide a custom code for this, it is not something that Views supports out of box like this.
Even with the custom code, still you need 4 inputs in the search or query (at the very least)

I rather suggest to (if you want to avoid coding) using more filter handles instead of just one.

#950062

Let's say I add 2 text filters at front:
- weekend_current_time
- workday_current_time

And 4 filters at back:
working-days-open < workday_current_time
working-days-close > workday_current_time
friday-open < weekend_current_time
friday-close > weekend_current_time

And then I hide these filters at front and show checkbox, that when checked, would write current time in either weekend_current_time or workday_current_time

Would this work?

#952073

I think the simplest is this:

1. Populate your fields possibly with numeric values only (1130 instead of 11:30)
2. Create a View, that has the Query Filters by those Fields (not front end but backend query filters)
==> You can have them as URL parameter or even shortcode parameter in the View later
==> The query filters should check Something like "working-days-open is a UNSIGNED greater than VIEW_PARAM(startweek)" (or URL parameter, if you prefer so)
3. If you use a ShortCode attribute you can populate that with a custom shortcode (do not forget to register it in Toolset):

function make_current_hour_string( $atts ) {
$hour_now = date( 'Hi');//Here we transform the NOW() to an hour of 11:20 format (so, not today, but any day this time. I tried both H:i and Hi, changing correspondingly the values in my fields)
return $hour_now;
}
add_shortcode( 'now_to_hour_and_minute_format', 'make_current_hour_string' );

You use the View like this:
[wpv-view name="view-by-working-hours-now" startweek="[now_to_hour_and_minute_format]" ]

That is applying just one of the 4 fields filter and applies no custom logic to check if it's Friday.
But it should give an idea how you should proceed.

==> I tested this both with 11:20 format and 1120 format (with and without 🙂 and it seems to work even if you add the :
I although do not suggest that as it'll be a mixed value to compare.

You can now use this for example to reload the view with a set URL parameter after you click a simple link anywhere on the view which loads the page with the view, adding URL parameters as passed by the Shortcode 😉

You could add them all in one button or several filters (opening now, even closing soon, etc etc)