getResources call syntax to filter a date TV to exclude past dates

Looking for clarification of the answer given by the very helpful halftrainedharry in Jan 2021 to the same question. The syntax in his answer doesn’t work for me. I am guessing it is a syntax problem:

Using runSnippet to call getResources after this line:

$currentdate = date('d-m-Y');

Then these lines in the $scriptProperties array:

'sortbyTV' => 'eventdate',
'sortbyTVType' => 'datetime',
'sortdirTV' => 'ASC',
'tvFilters' => 'eventdate>>$currentdate',

That returns all resources with no filtering.

By contrast, this syntax from the Jan 2021 answer returns no resources:

'tvFilters' => 'eventdate>>'.$currentdate,

If this is a syntax error, what is the correct syntax to filter the dates to exclude dates now in the past?

The getResources documentation doesn’t cover the inclusion of variables in TV filters, I believe.

The eventdate TV has output type selection: Date, and the Date Format: %d-%m-%Y

Could you link the topic in question?


TV values of type “date” are all stored in the same format in the database.
I guess for the query you have to use this universal format and not your custom one.

I think this is the link to the 2021 thread:

Thank you very much for the reply.

So is the problem with d-m-Y below? If so, any idea what it should be?

$currentdate = date('d-m-Y');

Use the format from the topic you linked → date('Y-m-d H:i:s')


Also there exist other ways to achieve the same:

1 Like

&tvFilters=event_start>=[[!+nowdate:default=now:date=%Y-%m-%d]]``

Is nowdate a snippet or is that a variable that MODX already knows how to work with?

Tried this:

‘tvFilters’ => ‘event_start>=[[!+nowdate:default=now:date=%Y-%m-%d]]’

But it returns nothing.

It’s a bit of a hack. [[!+nowdate]] is technically a placeholder that doesn’t exist, so the default value of the output modifier will be used, which gets you the current datetime (that you then can format with date).


Did you change event_start to the actual name of your TV?
Can you maybe post the whole getResources snippet call you use?


$scriptProperties = array(
    'parents' => '10,14,32,36',
    'depth' => '1',
    'limit' => '60',
    'showHidden' => '0',
    'includeTVs' => '1',
    'processTVs' => '1',
    'tvPrefix' => '',
    'sortbyTV' => 'eventdate',
    'sortbyTVType' => 'datetime',
    'sortdirTV' => 'ASC',
    'tvFilters' => 'eventdate>=[[!+nowdate:default=now:date=%Y-%m-%d]]',
    'includeContent' => '0',
    'tpl' => 'teaser'
);
$output .= $modx->runSnippet('getResources', $scriptProperties);
return $output;

As it stands, it returns all the event resources with no filtering so we see events in the past.

I thought about writing a script from scratch, and it seems simple to eliminate the past event resources, but then I have no clue how to order the array of resources in chronological order:

$c = $modx->newQuery('modResource');
$c->where(array(
   'parent:IN' => array(10,36,14,32),
   'deleted' => false,
   'hidemenu' => false,
   'published' => true,
));
$c->sortby('menuindex','ASC');
$c->limit(1000);
$resArray = $modx->getCollection('modResource',$c);

$output = '';
foreach($resArray as $res) {
$date  = $res->getTVValue('date');

$startDate = strtotime(date('d-m-Y', strtotime($date)));
$currentDate = strtotime(date('d-m-Y'));

if($startDate > $currentDate) {.....

OK I see the confusion. There are two ways to achieve the filtering.

The way you posted is when you create a custom snippet with PHP code, that then calls getResources (with $modx->runSnippet('getResources')).
Here you can’t use other MODX tags (like [[!+nowdate:default=`now`:date=`%Y-%m-%d`]] ) in the PHP code.


The other way is to call the snippet getResources directly in your page content or chunk (without creating a custom snippet).
Here nested MODX tags (like [[!+nowdate:default=`now`:date=`%Y-%m-%d`]] or [[!getDate]]) can be used.

I wouldn’t try to reinvent the wheel.
It’s quite a common use case to filter resources by a TV of type date, and getResources/pdoResources can both be used for this.

1 Like

Will try rewriting the snippet call in the template without the runSnippet.

Want to thank you for all the help. At the moment nothing is working, and it is probably a good thing I live on the ground floor. Will need to take a break to save what little is left of my sanity.

“Learn to code,” they said.

!?

It turns out that the crucial problem was what you initially mentioned: the format of the date in the nowdate snippet that is called by the getResources call:

So the getResources call that works is:

[[!getResources? 
&tpl=`teaser` 
&parents=`10,14,32,36` 
&includeTVs=`1` 
&processTVs=`1` 
&tvPrefix=`` 
&includeContent=`0` 
&showHidden=`0`
&limit=`60`   
&sortbyTV=`eventdate`  
&sortbyTVType=`datetime`  
&sortdirTV=`ASC` 
&tvFilters=`eventdate>>[[!nowdate]]`]]

And nowdate is just what it was in January of 2021:

return date('Y-m-d H:i:s');

The getResources call works if the nowdate snippet just uses Y-m-d but it does NOT work with d-m-Y, which is what I was using as the output format for the eventdate template variable.

Thank you again Harry.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.