A frequently encountered question on the forum is how to show pages that fall within a specified time period e.g.
a. List 4 pages of the same month as the current page
b. List events for current year
etc.

The cms:pages tag we use to list pages supports two parameter - 'start_on' and 'stop_before'. These can be set to any dates to effectively constrain the pages being listed to within the time-period delineated by them (IMP: the format of dates as accepted by these parameters has to be yyyy-mm-dd). As an example -
<cms:pages
masterpage='some-template.php'
start_on='2014-07-01'
stop_before='2014-08-01'
show_future_entries='1'
>

In the code above, the cms:pages tag will only fetch pages that have been published in the month of July, 2014 (the code says - start from 1st day of July, 2014 and stop before 1st day of Aug, 2014 i.e. effectively remain with July, 2014).
Please notice that we have also set the 'show_future_entries' parameter to '1' else cms:pages will ignore pages that fall in the future.

It is, however, not always feasible to specify a literal date as the parameters. For example to show pages of only the current month we'll have to dynamically calculate the two dates from whatever is the current month.

For such cases we'll have to fall upon a little PHP.
PHP has a pretty useful function named 'strtotime' that accepts human-readable strings (e.g. 'last day of next month', 'Monday next week') and returns a timestamp representing the date (please see http://php.net/manual/en/datetime.formats.relative.php for complete examples).

We can then convert the timestamp returned by 'strtotime' to the yyyy-mm-dd format we need and use it with cms:pages tag.

As an example, the following snippet will calculate the dates of first day of the current year and the first day of the next year and stores them in Couch variables
Code: Select all
<cms:php>
    global $CTX;

    // calculate timestamps for the dates
    $timestamp_1 = strtotime("1/1 this year"); // first day of first month this year
    $timestamp_2 = strtotime("1/1 next year"); // first day of first month next year
   
    // set the timestamps as Couch variables (my_start_date and my_stop_date)
    $CTX->set( 'my_start_date', date('Y-m-d', $timestamp_1), 'global' );
    $CTX->set( 'my_stop_date', date('Y-m-d', $timestamp_2), 'global' );
</cms:php>

So now if we can access the dates as regular Couch variables e.g. -
Code: Select all
<cms:show my_start_date /> 
<cms:show my_stop_date />

Or (more relevantly) use them as parameters of cms:pages tag e.g. as follows -
<cms:pages
masterpage='some-template.php'
start_on=my_start_date
stop_before=my_stop_date
show_future_entries='1'
>

Following are some of the threads on the forum that use variations of the mentioned technique -

Limit future entries by xx days or months
viewtopic.php?f=4&t=8171

How to list 4 pages of the same month as the current page
viewtopic.php?f=2&t=8194

using editable region datetime and fetching current month
viewtopic.php?f=4&t=8211

How to calculate difference between 2 dates
viewtopic.php?f=4&t=8226

List events for current year
viewtopic.php?f=4&t=8313

Show only future posts or only past events
viewtopic.php?p=15310#p15310

Date 'n' number of days from now
viewtopic.php?p=21965#p21965

Hope this helps.