Problems, need help? Have a tip or advice? Post it here.
3 posts Page 1 of 1
A couple of queries with dates. I am using two editable fields for date input and one of these dates is used as input to publish date:
Code: Select all
    <cms:editable type='datetime' validator='MyDate::set_default_date' name='date_range_start' format='dmy' allow_time='1' minute_steps='5' default_time='@current' label="Enter single date OR earliest date in range OR first of several dates" desc="set HH:MM to 00:00 if time is not known or you don't want it shown"  order='15' />


kfunctions.php:
Code: Select all
class MyDate{
    function set_default_date( $field ){
        global $FUNCS;
       
        $date_range_start = trim($field->get_data());
       
        // Set publish_date to value of this field
        $f = &$field->siblings[3];
        $publish_date = trim( $f->get_data() );
        if( $publish_date != '0000-00-00 00:00:00' ){
            $date_range_start = $FUNCS->make_date( $date_range_start );
            if( $publish_date != $date_range_start ){
                $f->store_posted_changes( $date_range_start );
            }
        }
       
        unset( $f );
       
        return true;
    }
}


My first question is how I could check whether, if the 2nd date is entered that it is later than the 1st date - if not - throw an error message. I've looked through tips & tricks and don't think the answer is there.

The second question is about the default_time='@current' - the date and HH default OK to current date/time, BUT MM remains blank. I wonder why? Ideally what I would like is for the date (DD MM YY) to default to current date and for HH and MM to default to 00 and 00. (the reason for this is that much of the time the client will be setting the HH and MM to 00 if they do not want the time element displayed).
how I could check whether, if the 2nd date is entered that it is later than the 1st date - if not - throw an error message
Please add the 'check_end_date' function given below into your class -

Code: Select all
class MyDate{
    ..
    ..
   
    function check_end_date( $field ){
        global $FUNCS;

        $end_date = trim($field->get_data());

        $f = &$field->siblings[3]; // get publish_date
        $publish_date = trim( $f->get_data() );
        if( $publish_date != '0000-00-00 00:00:00' ){
            $end_date = $FUNCS->make_date( $end_date );
            if( $end_date < $publish_date ){
                return KFuncs::raise_error( "End date cannot be smaller than the start date" );
            }
        }
        unset( $f );

        return true;
    }
}

You can now add 'MyDate::check_end_date' validator to your second date field.

As an aside - since the first date is being set as the publish date, we are checking the second date above against the publish date.
As an alternative, we could have extracted the first date from the first field itself - assuming its name is 'start_date', following bit of code would have fetched that for us -
Code: Select all
 // Get start date
$f = &$field->page->_fields['start_date'];
$start_date = trim( $f->get_data() );

The second question is about the default_time='@current' - the date and HH default OK to current date/time, BUT MM remains blank. I wonder why?

I think that is because you have set the minute interval to '5' - if the current minute happens not be cleanly divisible by 5 it would be rejected leading to the blank. Please try using an interval of '1' which will make all the values acceptable.

Hope this helps.
thank you @kk - problems solved - many thanks for your time and help :)
3 posts Page 1 of 1