Using ZendX_JQuery_Form_Element_DatePicker to find the current Server Time

3.5k views Asked by At

Iam using ZendX_JQuery_Form_Element_DatePicker, I want to set the a default date to the date time picker.

My current code is this -

$date = new ZendX_JQuery_Form_Element_DatePicker('date',
                array('jQueryParams' => array('defaultDate' => date('Y-m-D'),
                                'changeYear'=> 'true')));
        $date->setJQueryParam('dateFormat', 'dd.mm.yy')
                ->setRequired()
                ->setLabel("Date");

This returns the date as August 2016 on the calender. Iam not sure what am doing wrong. Can someone suggest here the code for getting the correct server date.

3

There are 3 answers

0
Marcin On BEST ANSWER

I would recommend considering using JQuery without ZendX_JQuery (if possible). The reason is that currently there is discussion about Discontinuing Maintenance of ZendX JQuery - Suggest drop for 2.0.

If you do not use ZendX_JQuery, then the default date could be set during construction of your Zend_From. For example:

 $dateInput = $this->createElement('text', 'date');
 $dateInput->addValidator(new Zend_Validate_Date(array('format' => 'dd.MM.yyyy')));
 $dateInput->setValue(Zend_Date::now()->toString('dd.MM.yyyy'));

Then in your view, you could add:

<script language="JavaScript">
    // assummmig date text input field has id="date"
    // datepicker automatically will set itself to the current value in the field
    // dateFormat seems different, but this is because there are some differences
    // between formats in Zend_Data and JQuery.
    $( "#date" ).datepicker({ dateFormat: 'dd.mm.yy' });
</script>

Nevertheless, returning to your code I think the defaultDate should be in the current dateFormat (from datepicker doc). In your code, it is not, so this may be one reason it does not work properly.

0
sudol On

Try

array('defaultDate' => date('d-m-y')

Instead of

array('defaultDate' => date('Y-m-D')

1
ehudokai On

Try using Zend_Date()

so the line would look like

 array('jQueryParams' => array('defaultDate' => new Zend_Date(),

Hope that helps!