Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
13 posts Page 1 of 2
UPDATE: Relative dates and date comparison examples re-published at Midware Tags Reference » date

________________________

Hello,
I introduce a mod to CouchCMS <cms:date /> tag. It includes a new parameter, from_custom, which accepts date formats identical to what existing parameter format accepts.

Sample usage - <cms:date date=mydate format="d.m.Y" from_custom='j F, Y' />

Edit: added new parameter "return", see posts below.
@KK, if you ever read this thread - please consider posting a tutorial on how to alter couch tags without pasting the whole code of the function in kfunctions.php. I would love to use this technique( if it is possible ) to alter much bigger tags - like cms:pages etc..
--
Hey all, I took some time today to add return parameter, an adaptation of php function "strtotime". It can be used like following samples with cms:date tag.
Code: Select all
Current date: <cms:date format='Y-m-d H:i:s' /><br/><br/>
Current -365 days: <cms:date return='-365 days' /><br/><br/>
Current +1 day: <cms:date return='+1 day' /><br/><br/>
Current +1 week: <cms:date return='+1 week' /><br/><br/>
Current +1 month: <cms:date return='+1 month' /><br/><br/>
Current +1 day 4 hours 2 seconds: <cms:date return='+1 day 4 hours 2 seconds' /><br/><br/>
Current +1 week 2 days 4 hours 2 seconds with another format: <cms:date return='+1 week 2 days 4 hours 2 seconds' format='Y-m-d H:i:s' /><br/><br/>
First day of current week: <cms:date return='Monday this week' /><br/><br/>
Last day of previous week: <cms:date return='Sunday last week' /><br/><br/>
Next Thursday: <cms:date return='next Thursday' /><br/><br/>
Last Monday: <cms:date return='last Monday' /><br/><br/>
First day of next month: <cms:date return='first day of next month' /><br/><br/>
First day of next month: <cms:date return='first day of next month' format='l' /><br/><br/>
Last day of march 2009: <cms:date '2009-03' return='last day of this month' format='l, j/n/Y' /><br/><br/>
Christmas Day next year: <cms:date '25-12' return='next year' format='l, j/n/Y' from_custom='d-m' /><br/><br/>
Yesterday 14:00: <cms:date return='yesterday 14:00' format='j F, Y H:i'/><br/><br/>
Tomorrow: <cms:date return='tomorrow' format='Y-m-d' /><br/><br/>
Tomorrow noon: <cms:date return='tomorrow noon' format='j F, Y H:i' /><br/><br/>
A minute before midnight: <cms:date return='midnight -1 minute' format='H:i' /><br/><br/>
All days of my bitrthday:
<cms:repeat '5' startcount='0' >
   <br/><cms:date '2016-09-16' return="+<cms:show k_count /> years" format='l, j/n/Y' />
</cms:repeat>

If pasted, above code produces the following output:

Current date: 2016-11-23 12:08:34

Current -365 days: November 24, 2015

Current +1 day: November 24, 2016

Current +1 week: November 30, 2016

Current +1 month: December 23, 2016

Current +1 day 4 hours 2 seconds: November 24, 2016

Current +1 week 2 days 4 hours 2 seconds with another format: 2016-12-02 16:08:36

First day of current week: November 21, 2016

Last day of previous week: November 20, 2016

Next Thursday: November 24, 2016

Last Monday: November 21, 2016

First day of next month: December 01, 2016

First day of next month: Thursday

Last day of march 2009: Tuesday, 31/3/2009

Christmas Day next year: Monday, 25/12/2017

Yesterday 14:00: 22 November, 2016 14:00

Tomorrow: 2016-11-24

Tomorrow noon: 24 November, 2016 12:00

A minute before midnight: 23:59

All days of my bitrthday:
Friday, 16/9/2016
Saturday, 16/9/2017
Sunday, 16/9/2018
Monday, 16/9/2019
Wednesday, 16/9/2020
Thanks trendoman

Will make relative dates much easier and tidier code!!
@trendoman,

As it happens, we don't need an addon to do those things - the core <cms:date> is capable of doing it by itself.
Try the following -
Code: Select all
Current date: <cms:date format='Y-m-d H:i:s' /><br/><br/>
Current -365 days: <cms:date '-365 days' /><br/><br/>
Current +1 day: <cms:date '+1 day' /><br/><br/>
Current +1 week: <cms:date '+1 week' /><br/><br/>
Current +1 month: <cms:date '+1 month' /><br/><br/>
Current +1 day 4 hours 2 seconds: <cms:date '+1 day 4 hours 2 seconds' /><br/><br/>
Current +1 week 2 days 4 hours 2 seconds with another format: <cms:date '+1 week 2 days 4 hours 2 seconds' format='Y-m-d H:i:s' /><br/><br/>
First day of current week: <cms:date 'Monday this week' /><br/><br/>
Last day of previous week: <cms:date 'Sunday last week' /><br/><br/>
Next Thursday: <cms:date 'next Thursday' /><br/><br/>
Last Monday: <cms:date 'last Monday' /><br/><br/>
First day of next month: <cms:date 'first day of next month' /><br/><br/>
First day of next month: <cms:date 'first day of next month' format='l' /><br/><br/>
Last day of march 2009: <cms:date '2009-03 last day of this month' format='l, j/n/Y' /><br/><br/>
Christmas Day next year: <cms:date '25 december next year' format='l, j/n/Y' /><br/><br/>
Yesterday 14:00: <cms:date 'yesterday 14:00' format='j F, Y H:i'/><br/><br/>
Tomorrow: <cms:date 'tomorrow' format='Y-m-d' /><br/><br/>
Tomorrow noon: <cms:date 'tomorrow noon' format='j F, Y H:i' /><br/><br/>
A minute before midnight: <cms:date 'midnight -1 minute' format='H:i' /><br/><br/>
All days of my bitrthday:
<cms:repeat '5' startcount='0' >
   <br/><cms:date "2016-09-16 +<cms:show k_count /> years" format='l, j/n/Y' />
</cms:repeat>

With the date of this post being 30th of July 2017, following are the results -
Current date: 2017-07-30 17:02:25

Current -365 days: July 30, 2016

Current +1 day: July 31, 2017

Current +1 week: August 06, 2017

Current +1 month: August 30, 2017

Current +1 day 4 hours 2 seconds: July 31, 2017

Current +1 week 2 days 4 hours 2 seconds with another format: 2017-08-08 11:32:27

First day of current week: July 31, 2017

Last day of previous week: July 30, 2017

Next Thursday: August 03, 2017

Last Monday: July 24, 2017

First day of next month: August 01, 2017

First day of next month: Tuesday

Last day of march 2009: Tuesday, 31/3/2009

Christmas Day next year: Tuesday, 25/12/2018

Yesterday 14:00: 29 July, 2017 14:00

Tomorrow: 2017-07-31

Tomorrow noon: 31 July, 2017 12:00

A minute before midnight: 23:59

All days of my bitrthday:
Friday, 16/9/2016
Saturday, 16/9/2017
Sunday, 16/9/2018
Monday, 16/9/2019
Wednesday, 16/9/2020

BTW, the formats of such 'relative dates' can be found here -
http://php.net/manual/en/datetime.formats.relative.php
Thanks, KK!
Please still keep this topic and don't delete it, because addon also has from_custom param which has proved its effectiveness with various incoming date formats.
don't delete it
On the contrary - if you might have noticed, I had actually made the post sticky :)
There is a time discrepancy in the output of following:
Code: Select all

time now:<br>
<cms:date format='Y-m-d H:i:s' /><br>
regular cms:date:<br>
<cms:date 'now' format='Y-m-d H:i:s' /><br>
<cms:date '15 minutes ago' format='Y-m-d H:i:s' /><br>
with 'return':<br>
<cms:date format='Y-m-d H:i:s' return='now' /><br>
<cms:date format='Y-m-d H:i:s' return='15 minutes ago' /><br>


I receive correct time using my mod and incorrect without..
Code that prints names of weekdays (for building dropdown/checkbox/radio opt_values change "<br/>" to "||" or "|")
Code: Select all
<cms:repeat count='7' ><cms:date "2018-01-01 +<cms:show k_count /> day" format='l' /><br/></cms:repeat>
<!--
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
-->


Crafted a function that returns any weekday:
Code: Select all
<cms:call 'print_weekday' /><!-- current (today's) weekday --><br/>
<cms:call 'print_weekday' day='1' /><!-- Monday--><br/>
<cms:call 'print_weekday' day='2' /><!-- Tuesday--><br/>
<cms:call 'print_weekday' day='' /><!-- Sunday (day='0' or day='' or day='7') --><br/>
13 posts Page 1 of 2