Pagesetter » Hack: date math for filters

Information

Code Snippets

I wanted to be able to filter publications based on a given number of days before or after today's date. Filters currently support @now as a way of filtering for events from "today", but no math allowed :(

So here's the hack:

in pnuserapi.php, around line 1565, change:

Code

if ($value == \@now')
$value = date('
Y-m-d');

to

Code

if (substr($value,0,4) == \@now'){
            if ($value == \@now'
){
                $value = date('Y-m-d');
            } else if (substr($value,4,1) == '.'){
                $value  = date('Y-m-d',mktime(0, 0, 0, date("m")  , date("d")+substr($value,5,10), date("Y")));
            } else if (substr($value,4,1) == '-'){
                $value  = date('Y-m-d',mktime(0, 0, 0, date("m")  , date("d")-substr($value,5,10), date("Y")));
            }
          }

Translated, the hack - which uses the operators "-" and "." - only applies when the filter value starts with @now. If it does, and if it is only @now, it works as before. If the . operator follows @now, it does date math by adding what trails the . to today's date. If the - operator follows @now, it does date math by subtracting what trails the - from today's date.

Examples:

where tid = 2 & the date field to filter on is eventDate:

index.php?module=pagesetter&func=main&tid=2&filter=eventDate:gt:@now-2

This finds all dates greater than today minus 2. All dates newer than the day before yesterday. gt = greater than, @now = today, - is the operator, 2 is the number of days to subtract.

index.php?module=pagesetter&func=main&tid=2&filter=eventDate:lt:@now.30

This finds all dates less than 30 days from today. lt = less than, @now is today, . is the plus operator, and 30 days is the number to add.

Why use "." as a plus operator rather than "+"? "+" seems to get cleaned from input from the URL, so it won't work. "." is the nearest thing I can think of, as it is what you use to join values in various programming languages. :-D

Credits: Bronto
Alleen ingelogde gebruikers kunnen commentaar plaatsen Inloggen / aanmelden