On 14/04/2009 10:29 AM, Daryl Handley wrote:
Daryl Handley wrote:

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;
}
I just realized that your date is not in select boxes, so you could
actually just do

function getDate() {
$date = $this->getValue('date');
return new Zend_Date($startDate, "yyyy-MM-dd");
}


Whoa whoa, when creating a new Zend_Date object from a string, the second parameter is a string identifying how to parse the date passed in, _not_ the desired output format.

So, if you needed to interpret a date that is in dd/mm/yyyy and make it yyyy/mm/dd, you'd do something like this:

$date = new Zend_Date('03/12/2008', 'dd.MM.yyyy');
$string = $date->toString('yyyy/MM/dd')

Reply via email to