1. Is there a way to do this e.g. with Zend Date?
2. I do this in every controller. Is there a way to write a filter or
something that could handle this? Or maybe extend my Zend_Form?

I write a lot of reports that involve a start date and an end date. I use the jquery date picker from the zendx package which will put the date in mm/dd/yyyy format in a text box. All forms are instances of or extend my DateRange form. The date range form has 2 methods getStartDate and getEndDate to grab the start and end date very easily.

Here is my date range form.


===========================================

require_once APPLICATION_PATH . '/forms/BaseForm.php';

class DateRangeForm extends ZendX_JQueryForm {

   private $dateFormat = 'MM/dd/yyyy';
   /**
    */
   public function init() {
       parent::init();
// set the method for the display form to POST
       $this->setMethod('get');
// add start date and end date $today = new Zend_Date(); $todayString = $today->toString($this->dateFormat); $startDateElement = new ZendX_JQuery_Form_Element_DatePicker('startDate');
       $startDateElement->setValue($todayString);
       $startDateElement->setLabel('Start Date');
$startDateElement->setDescription("(mm/dd/yyyy)"); $startDateElement->setRequired(true); $startDateElement->addValidator(new Zend_Validate_Date($this->dateFormat));
       $this->addElement($startDateElement);

$endDateElement = new ZendX_JQuery_Form_Element_DatePicker('endDate');
       $endDateElement->setLabel('End Date');
$endDateElement->setDescription("(mm/dd/yyyy)"); $endDateElement->setValue($todayString); $endDateElement->setRequired(true); $endDateElement->addValidator(new Zend_Validate_Date($this->dateFormat)); $this->addElement($endDateElement);

       // add the submit button
       $this->addElement('submit', 'submit', array(
           'label'    => 'Submit',
       ));
   }
function getStartDate() {
       $startDate = $this->getValue('startDate');
       return new Zend_Date($startDate, $this->dateFormat);
   }
function getEndDate() {
       $endDate = $this->getValue('endDate');
       return new Zend_Date($endDate, $this->dateFormat);
} }

===========================================


If you want to stick with your format you could do something like this (where day, month, and year are the select boxes in your form).

function getDate() {
       $endDate = $this->getValue('endDate');
       $date = new Zend_Date();
       $date->setDay($this->getValue('day'));
       $date->setMonth($this->getValue('month'));
       $date->setYear($this->getValue('year'));
       return $date;
}

Reply via email to