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

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 -

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
    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' );

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 -

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

Limit future entries by xx days or months

How to list 4 pages of the same month as the current page

using editable region datetime and fetching current month

How to calculate difference between 2 dates

List events for current year

Show only future posts or only past events

Date 'n' number of days from now

Hope this helps.