Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
3 posts Page 1 of 1
Hi, this is a way to see the % of completion of progress based on difference between dates.

If some project starts at date1, then ends at date2 then we know the total number of days between these days.
Given any 3rd point, like 'today' - we can measure the progress with formula

start: 0% | ------- ---------- ---------------| 70% | ------------------| 100% end
date1 ----------------------- some intermediate date3 ------------ date2

( date3 - date1 ) / ( date2 - date1 ) * 100 = progress in %.

Installation follows in kfunctions.php:

Code: Select all
$FUNCS->register_tag( 'date_progress', array('CustomTags', 'get_date_progress') );


class CustomTags{
   
   
    function get_date_progress( $params, $node ){
        global $CTX, $FUNCS;
        if( count($node->children) ) die("ERROR: Tag \"".$node->name."\" is a self closing tag");

        extract( $FUNCS->get_named_vars( // any keyword that you can remember and use :)
                    array(
                        $from = array( 'from', 'start', 'begin' ),
                        $to = array( 'to', 'stop', 'end', 'finish', 'complete' ),
                        $boundary = array( 'cutoff', 'fold', 'threshold', 'boundary', 'current', 'limit' )
                          ),
                    $params)
               );

        if( count($params)>3 || count($params)<2 )die("ERROR: Invalid number of parameters in tag \"".$node->name."\"" );       

        $pos_from = key( array_intersect( array_column($params, 'lhs'), $from) );
        $pos_to = key( array_intersect( array_column($params, 'lhs'), $to) );
        $pos_boundary = key( array_intersect( array_column($params, 'lhs'), $boundary) );
       
        if( $pos_from === false )die("ERROR: Tag \"".$node->name."\" should have a starting date parameter: '" . implode( "' or '", $from ) . "'." );
        if( $pos_to === false ) die("ERROR: Tag \"".$node->name."\" should have an ending date parameter: '" . implode( "' or '", $to ) . "'.");
       
        $date1 = DateTime::createFromFormat( 'Y-m-d', $params[$pos_from]['rhs'] ); // starting date
        $date2 = DateTime::createFromFormat( 'Y-m-d', $params[$pos_to]['rhs'] );  // ending date
        if( $pos_boundary ){
            $date3 = DateTime::createFromFormat( 'Y-m-d', $params[$pos_boundary]['rhs'] );
        }else{
            $date3 = new DateTime('now'); $date3->format('Y-m-d');
        }
       
        if( $date1 == false ){ // 'from' error
            die("ERROR: Tag \"".$node->name."\": invalid format of '" . array_column($params, 'lhs')[$pos_from] . "' parameter. Correct format is 'Y-m-d'.");
        }elseif( $date2 == false ){ // 'to' error
            die("ERROR: Tag \"".$node->name."\": invalid format of '" . array_column($params, 'lhs')[$pos_to] . "' parameter. Correct format is 'Y-m-d'.");
        }elseif( $pos_boundary && $date3 == false ){ // user-provided 'boundary' error
            die("ERROR: Tag \"".$node->name."\": invalid format of '" . array_column($params, 'lhs')[$pos_boundary] . "' parameter. Correct format is 'Y-m-d'.");
        }elseif( $date3 == false ){ // 'boundary' error
            die("ERROR: Tag \"".$node->name."\": unable to get current time.");
        }else{ // all good
           
            $diff = $date1->diff($date2)->format("%r%a"); // ex.: total 300 days of project
            $diff_boundary = $date1->diff($date3)->format("%r%a"); // ex.: total: 150 days of project up to today
           
            if( $diff_boundary <= 0 ){
                $res = '0';
                return( $res );
            }
            if( $diff <= 0 ){
                $res = '100';
                return( $res );
            }
            if( $diff_boundary >= $diff ){
                $res = '100';
                return( $res );
            }
           
            $res = $diff_boundary / $diff * 100; // 150 / 300 * 100 = 50%
            $res = number_format($res, 0, '.', ',');
            return( $res );
        }
    }
 
}



The main novelty of this tag is synonyms for parameters.
Sample usage: <cms:date_progress from=date1 to=date2 boundary=date3 />

Parameter boundary is optional. If it is not set - then today's date is used.

Synonyms: Instead of remembering 'from' and 'to' - feel free to use any of the following:
'from', 'start', 'begin' - for indicating starting date
'to', 'stop', 'end', 'finish', 'complete' - for indicating final date
'cutoff', 'fold', 'threshold', 'boundary', 'current', 'limit' - for indicating the boundary.

The same way can be added labels in your own language( if it uses latin caracters ).
So, code and have no stress :lol:
Ok man, to be honest, this help me :D
nice job mate!
As soon as possible!

Touch me up : abada[dot]zulma[at]gmail[dot]com
GoingMarryAsap wrote: Ok man, to be honest, this help me :D
nice job mate!

Thank you for the feedback :D
3 posts Page 1 of 1

Who is online

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

Users browsing this forum: CouchBob and 2 guests