Posted on by

Filtering a List Based on the Current Date

It’s possible to determine what records are shown using the [pdb_list] shortcode by using the “filter” attribute. If you want to show records from a range of dates, you can use the filter to determine the start and end dates for the range.

List filters are discussed in detail here. Read that page if you have not worked with list filters before.

Note: I have added a general feature to Participants Database that makes many of the most common filters using dynamic dates possible without any additional code. Take a look at the documentation here to see if you can use this new feature.

For some more complex examples beyond what is possible just using shortcode filters, read on…

Since this requires PHP, there are a couple of ways to go. If you’re familiar with using templates or creating shortcodes, you can do it that way. Another less technical approach is to use a plugin that lets you put PHP code in your content. However you go, you will need to know some simple PHP to customize this for whatever you want to do in your case.

Here is an example of showing all the records from the last week:

And here is an example of getting all the results in the current month:

These are good examples of how to do simple date manipulations using PHP.

Using PHP Snippets

There are plugins that let you place PHP in your content which can simplify getting a little bit of code (a “snippet”) on your page. To do that, you would take the code, like one of the examples above, and place it in a snippet. Usually, you don’t need the PHP tags and then put the code in your page according to the instructions for the snippet plugin.

7 thoughts on “Filtering a List Based on the Current Date

  1. It’s been a while since I have looked at this feature. I recommend you try using the “current_day” key, which gives the current date at 12am. For example:

    [pdb_list filter="date_recorded>current_day"]

    This will give you all of today’s registrations.

    1. Thanks Roland!
      By using “>current_day”, now page is displaying only records that created today. Many thanks!.

  2. Thanks for your questions. The answer is simple, I should have noticed this before, but in your filter, you’re using “=” which is looking for an exact match (including the time of day), but you want to match the whole day, no matter the time, so you should try using “~” which will give you a date match ignoring the time. For example:

    [pdb_list filter="date_recorded~current_date"]

    I did not explain this in the documentation, I’ll fix that now.

    1. Still the same issue. after using “~” instead of “=”

      1. I just created new entries. 5-oct-2022, 4am
      2. Page is still showing the both 4-oct-22 + 5-oct-22 together.
      3. Another new observation: Below list view also displaying the same result when i did search on “date_record” with value “October 5, 2022”

      [pdb_list template=edit-link search="true" sort="true"]
      This is explicit search on value rather a variable like ‘current_date’. still application is showing similar results means, something else is causing the issue. Any thoughts?

  3. Thanks for the great plugin.

    i am not a coding expert, i am trying to make a light weight patient entry web application for our non-profit org. so i started learning things and doing my best to get the job done.

    one of my requirement is to have a page that display the all patient records submitted today. I am currently using the below shortcode for that purpose.

    [pdb_list filter="date_recorded=current_date"]

    But, it is still listing the data from previous day when i do it morning times. so, i suspected the system date conflict is causing this issue. I read your article (https://xnau.com/date-shifting-and-other-timezone-related-issues-in-participants-database/) about date settings and made the wordpress date to match my local time. Now created new records and they are having right date timestamps now. But…

    Here is my observations:

    1) After i made the WP date adjusted, i see the old records ‘date_recorded’ values are adjusted to new date preference (EST to IST values converted. example some records were adjusted from 27/9/2022 2:30am to 27/9/2022 5am). I think system should not update the old records values when WP date updated. the new setting to be applied to new record creations only i guess.

    2) Still above shortcode is not displaying current day (WP, and my local date are same now) records. Could you please advise me a code to have records displayed with current date.

    fyi, I am using Elementor page builder.

    1. Hi Raju,

      The time sync setting does not change anything in the database, it only changes the timezone that is used when displaying the date.

      You may have better results using the “current_day” key in the shortcode filter instead.

      1. Many thanks for your response Roland :-)
        It is still fetching the results from previous day. I am from India time. Page is able to display all the records created from 12.30PM India time.

        EXAMPLE:
        1. current date (India): 01-oct-2022
        2. Records created at 6am india time.
        3. They are having with my local timestamps. This good.
        4. But I am not able to view them on page with shortcode [pdb_list filter="date_recorded=current_date"].
        5. I did not made any changes in the settings are records.
        6. at sharp 12:30pm india time, page is displaying the records created today.

        I tried all combinations of timezone settings in wordpress&Paticipants Database. no use. and made PHP level date also pointing to India time. still no use.

        current_day is not giving results even after 12.30pm.

        So, I am suspecting something in plugin code related to current_date is causing issue. I have no idea in debugging it from code perspective. :-(

        Any idea whats going on with it?

        Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

Would you like to be notified of followup comments via e-mail? You can also subscribe without commenting.

7 thoughts on “Filtering a List Based on the Current Date

  1. It’s been a while since I have looked at this feature. I recommend you try using the “current_day” key, which gives the current date at 12am. For example:

    [pdb_list filter="date_recorded>current_day"]

    This will give you all of today’s registrations.

    1. Thanks Roland!
      By using “>current_day”, now page is displaying only records that created today. Many thanks!.

  2. Thanks for your questions. The answer is simple, I should have noticed this before, but in your filter, you’re using “=” which is looking for an exact match (including the time of day), but you want to match the whole day, no matter the time, so you should try using “~” which will give you a date match ignoring the time. For example:

    [pdb_list filter="date_recorded~current_date"]

    I did not explain this in the documentation, I’ll fix that now.

    1. Still the same issue. after using “~” instead of “=”

      1. I just created new entries. 5-oct-2022, 4am
      2. Page is still showing the both 4-oct-22 + 5-oct-22 together.
      3. Another new observation: Below list view also displaying the same result when i did search on “date_record” with value “October 5, 2022”

      [pdb_list template=edit-link search="true" sort="true"]
      This is explicit search on value rather a variable like ‘current_date’. still application is showing similar results means, something else is causing the issue. Any thoughts?

  3. Thanks for the great plugin.

    i am not a coding expert, i am trying to make a light weight patient entry web application for our non-profit org. so i started learning things and doing my best to get the job done.

    one of my requirement is to have a page that display the all patient records submitted today. I am currently using the below shortcode for that purpose.

    [pdb_list filter="date_recorded=current_date"]

    But, it is still listing the data from previous day when i do it morning times. so, i suspected the system date conflict is causing this issue. I read your article (https://xnau.com/date-shifting-and-other-timezone-related-issues-in-participants-database/) about date settings and made the wordpress date to match my local time. Now created new records and they are having right date timestamps now. But…

    Here is my observations:

    1) After i made the WP date adjusted, i see the old records ‘date_recorded’ values are adjusted to new date preference (EST to IST values converted. example some records were adjusted from 27/9/2022 2:30am to 27/9/2022 5am). I think system should not update the old records values when WP date updated. the new setting to be applied to new record creations only i guess.

    2) Still above shortcode is not displaying current day (WP, and my local date are same now) records. Could you please advise me a code to have records displayed with current date.

    fyi, I am using Elementor page builder.

    1. Hi Raju,

      The time sync setting does not change anything in the database, it only changes the timezone that is used when displaying the date.

      You may have better results using the “current_day” key in the shortcode filter instead.

      1. Many thanks for your response Roland :-)
        It is still fetching the results from previous day. I am from India time. Page is able to display all the records created from 12.30PM India time.

        EXAMPLE:
        1. current date (India): 01-oct-2022
        2. Records created at 6am india time.
        3. They are having with my local timestamps. This good.
        4. But I am not able to view them on page with shortcode [pdb_list filter="date_recorded=current_date"].
        5. I did not made any changes in the settings are records.
        6. at sharp 12:30pm india time, page is displaying the records created today.

        I tried all combinations of timezone settings in wordpress&Paticipants Database. no use. and made PHP level date also pointing to India time. still no use.

        current_day is not giving results even after 12.30pm.

        So, I am suspecting something in plugin code related to current_date is causing issue. I have no idea in debugging it from code perspective. :-(

        Any idea whats going on with it?

        Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

Would you like to be notified of followup comments via e-mail? You can also subscribe without commenting.