The Types plugin allows you to add custom fields for date and time. This enables you to add query filters to your Views and return only the posts that fall within specific dates, or dates relative to the current one. You can use this for calendar projects, displaying orders or invoices for a specific period, and more.

The Types custom fields for date and time are stored in the database as UNIX timestamps, which are positive integers. Therefore, when inserting a query filter to test a date custom field you should use the NUMBER option.

The following image displays an example where a filter is used to only show posts whose “Deadline” is today.

Date filter dialog in Views
Date filter dialog in Views

In the query filter dialog, you specify the custom field to test, the type of comparison (e.g. “greater than”, “lower than or equal”, etc.), and the value to compare with. You could enter a timestamp directly, or use the following functions to generate one.

The following predefined functions dynamically generate timestamp values to be used in your date comparisons.

  • NOW() (the time at the moment in milliseconds)
  • TODAY() (time at 00:00 on today)
  • FUTURE_DAY(int) (time at 00:00 on the following day)
  • PAST_DAY(int) (time at 00:00 on the previous day)
  • THIS_MONTH() (time at 00:00 on first day of this month)
  • FUTURE_MONTH(int) (time at 00:00 on first day of future month)
  • PAST_MONTH(int) (time at 00:00 on first day of past month)
  • THIS_YEAR() (time at 00:00 on Jan 1st of this year)
  • FUTURE_YEAR(int) (time at 00:00 on Jan 1st of a future year)
  • PAST_YEAR(int) (time at 00:00 on Jan 1st of a past year)
  • SECONDS_FROM_NOW(int) (N seconds from the moment of execution)
  • MONTHS_FROM_NOW(int) (N months from the moment of execution, day of month is persisted)
  • YEARS_FROM_NOW(int) (N years from the moment of execution, day and month are the same)
  • DATE(dd,mm,yyyy) eg. DATE(21,10,2011)

Examples

Let’s say that the current date is 5th of May, 2012.

  • PAST_MONTH(2) will return 1st of March, 2012.
  • FUTURE_DAY(10) will be 15th of May, 2012
  • YEARS_FROM_NOW(-5) will result in 5th of May, 2007.

Notice that some of the functions return the first day or month of a year, while other functions keep the day/month value (you might need both depending on the project). Also, the _FROM_NOW(…) functions take positive or negative numbers as parameters so that you can calculate future or past dates.

Filtering Between Dates

Another handy use of the date filters is via the BETWEEN function. You can list all entries between two dates. Just select the BETWEEN function from the dropdown and list the two date functions below, separated with a comma. This would list all entries with a date custom field value between the first and the second function, for instance listing all records for the current month:

Filtering between data ranges
Filtering between data ranges