Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
11 posts Page 1 of 2
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' />

Installation is very simple - place the following into the couch/addons/kfunctions.php and it will override the default behavior( as suggested by @KK here https://www.couchcms.com/forum/viewtopi ... =10#p22187 ).

Edit: added new parameter "return", see posts below.

Code: Select all
$FUNCS->add_event_listener( 'alter_tag_date_execute', array('CustomTags', 'date_from_custom') );

class CustomTags{
   

    function date_from_custom( $tag_name, $params, $node, &$val ){
       
            global $CTX, $FUNCS, $PAGE;
            if( count($node->children) ) {die("ERROR: Tag \"".$node->name."\" is a self closing tag");}

            extract( $FUNCS->get_named_vars(
                        array(
                              'date'=>'',
                              'format'=>'F d, Y',
                              'from_custom'=>'',
                              'return'=>'',
                              'gmt'=>'0',
                              'locale'=>'',
                              'charset'=>'' /*charset to be converted to utf8 */
                              ),
                        $params)
                   );

            if( trim($date)=='' ){
                $date = $FUNCS->get_current_desktop_time();
            }elseif( $from_custom ){
                    $date = DateTime::createFromFormat( $from_custom, $date );
                    if( $date === false ) die("ERROR: Tag \"".$node->name."\": format of date in parameter 'from_custom' doesn't match the source date format.");
                    $date = $date->format('Y-m-d H:i:s');
            }
           
            $gmt = ( $gmt==1 ) ? 1 : 0;
            $locale = trim( $locale );
            $charset = trim( $charset );
            $return = trim( $return );

         
            if( $return && (($timestamp = strtotime( $return )) !== false) ){
                      $date = date( 'Y-m-d H:i:s', strtotime( $date . ' ' . $return ) );
            }elseif( $return ){
                      die("ERROR: Tag \"".$node->name."\": parameter \"return\" contains invalid value \"" . $return . "\".");
            }
         
            //TODO: localization
            $ts = ( $gmt ) ? @strtotime($date) - (K_GMT_OFFSET * 60 * 60) :  @strtotime($date);

            if( strpos($format, "%")===FALSE ){
                $val =  @date( $format, $ts );
            }
            else{// use strftime
                if( $locale ){
                    $orig_locale = setlocale(LC_ALL, "0");
                    @setlocale(LC_ALL, $locale);
                }

                $val = @strftime( $format, $ts );

                if( $locale ){
                    @setlocale(LC_ALL, $orig_locale);
                }
                if( $charset ){
                    if( function_exists('iconv') ){
                        $val = @iconv( $charset, 'UTF-8', $val );
                    }
                }

            }       
       
        return 1; // prevent the original tag from executing as we have set the output above

    }


}


Feel free to ask questions here or in Free Skype Chat :)
active topics, google
Free support is never free.. Donate!
@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..
active topics, google
Free support is never free.. Donate!
Replaced code in the first post with better error handling.
active topics, google
Free support is never free.. Donate!
Updated code in the first post ->

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
active topics, google
Free support is never free.. Donate!
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.
active topics, google
Free support is never free.. Donate!
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..
active topics, google
Free support is never free.. Donate!
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
-->


A function that returns any weekday:
Code: Select all
<cms:func 'print_weekday' day="<cms:add "<cms:date format='w' />" '1' />" >
    <cms:date "2018-01-01 +<cms:sub day '1' /> day" format='l' />
</cms:func>

<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/>
active topics, google
Free support is never free.. Donate!
11 posts Page 1 of 2

Who is online

In total there are 5 users online :: 0 registered, 0 hidden and 5 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 5 guests