getDate
Created by David Pede on Nov 28, 2013.
What is getDate?
A simple timestamp retrieval Snippet for MODX Revolution.
History
getDate was first written by David Pede (davidpede) and released on November 22, 2013.
Download
It can be downloaded from within the MODX Revolution manager via Package Management, or from the MODX Extras Repository, here: http://modx.com/extras/package/getDate
The source code and build script is also available on GitHub: https://github.com/tasianmedia/getDate
Bugs & Feature Requests
Bugs, issues and feature requests can be reported in the GitHub Repository, found here: https://github.com/tasianmedia/getDate/issues
Usage
The getDate snippet can be called using the tag:
[[!getDate]]
Available Properties
Selection Properties
Name | Description |
Default Value |
Added in Version |
---|---|---|---|
offset | The period of time to add or subtract from the current timestamp. Use relative date/time formats that the strtotime() parser understands. | now | 1.0.0-pl |
Example Offset | Example Snippet Call |
---|---|
+5 Hours | [[!getDate? &offset=`5 hour`]] |
+4 Days | [[!getDate? &offset=`4 day`]] |
+3 Weeks | [[!getDate? &offset=`3 weeks`]] |
+2 Months | [[!getDate? &offset=`2 month`]] |
+1 Year | [[!getDate? &offset=`1 year`]] |
-5 Hours | [[!getDate? &offset=`-5 hour`]] |
-4 Days | [[!getDate? &offset=`-4 day`]] |
-3 Weeks | [[!getDate? &offset=`-3 weeks`]] |
-2 Months | [[!getDate? &offset=`-2 month`]] |
-1 Year | [[!getDate? &offset=`-1 year`]] |
Date Format Options
Output Filters
getDate outputs a unix timestamp. To format this timestamp into a human friendly format, please use the built in MODX Output Filter 'date'.
Example Output | The Filter Parameters |
---|---|
22 November 2013 | [[!getDate:date=`%e %B %Y`]] |
Fri Nov 22, 2013 | [[!getDate:date=`%a %b %e, %Y`]] |
Friday, November 22, 2013 | [[!getDate:date=`%A, %B %e, %Y`]] |
2013-11-22 | [[!getDate:date=`%Y-%m-%d`]] |
Examples
Output the current unix timestamp:
[[!getDate]]
Output the current date (YYYY-MM-DD) using output filters:
[[!getDate:date=`%Y-%m-%d`]]
Output the date two months ago (YYYY-MM-DD) using output filters:
[[getDate:date=`%Y-%m-%d`? &offset=`-2 month`]]
Using getDate with getResources
When combined with getResources, getDate allows you to apply powerful date/time based filtering to your output. This includes filtering by date using resource fields and TV's.
Settings Resource Fields
The following examples use the '&where' parameter and setting resource fields.
Output only resources published within the last three months:
[[!getResources? &parents=`[[*id]]` &tpl=`myRowTpl` &where=`[{"publishedon:>=":"[[!getDate? &offset=`-3 month`]]"}]` ]]
Output only resources published from the start of last month:
[[!getResources? &parents=`[[*id]]` &tpl=`myRowTpl` &where=`[{"publishedon:>=":"[[!getDate? &offset=`first day of last month`]]"}]` ]]
Output only resources published in the last week and edited today:
[[!getResources? &parents=`[[*id]]` &tpl=`myRowTpl` &where=`[{"publishedon:>=":"[[!getDate? &offset=`-1 week`]]","editedon:>=":"[[!getDate? &offset=`today`]]"}]` ]]
Date Template Variable (TV)
The following examples use the '&tvFilters' parameter and Date TV's.
Output only resources with a custom date younger than three months ago:
[[!getResources? &parents=`[[*id]]` &tpl=`myRowTpl` &includeTVs=`1` &includeTVList=`myCustomDate` &tvFilters=`myCustomDate>=[[!getDate:date=`%Y-%m-%d`? &offset=`-3 month`]]` ]]
Output only resources with a custom date younger than the start of last month:
[[!getResources? &parents=`[[*id]]` &tpl=`myRowTpl` &includeTVs=`1` &includeTVList=`myCustomDate` &tvFilters=`myCustomDate>=[[!getDate:date=`%Y-%m-%d`? &offset=`first day of last month`]]` ]]
Output only resources where just the month of a custom date equals the current month:
[[!getResources? &parents=`[[*id]]` &tpl=`myRowTpl` &includeTVs=`1` &includeTVList=`myCustomDate` &tvFilters=`myCustomDate==[[!getDate:date=`%Y-%m-%`]]` ]]
Output only resources with one custom date younger than last week and a second custom date younger than midnight today:
[[!getResources? &parents=`[[*id]]` &tpl=`myRowTpl` &includeTVs=`1` &includeTVList=`myCustomDate01,myCustomDate02` &tvFilters=`myCustomDate01>=[[getDate:date=`%Y-%m-%d`? &offset=`-1 week`]],myCustomDate02>=[[getDate:date=`%Y-%m-%d %T`? &offset=`today`]]` ]]
Suggest an edit to this page on GitHub (Requires GitHub account. Opens a new window/tab).