1

I have trouble saving my date and time values. I tried different formats, here the actual try.

validator in my form:

 $datum=$this->CreateElement('text','datum')
                    ->setAttrib('size', '10')
                    ->addValidator(New Zend_Validate_Date('MM-DD-YYYY'));
$zeit=$this->CreateElement('text','zeit')
                    ->setAttrib('size', '10')

                    ->addValidator(new Zend_Validate_Date(array('format' => 'H:i:s')));

Snippet of my Controller addAction

if ($this->getRequest()->isPost()) {
    $formData = $this->getRequest()->getPost();
    if ($form->isValid($formData)) {
        $logenr = $this->_getParam('kopfnr', 0);
        $kopfnr = $logenr;
        $dat= $form->getValue('datum');
        $zeit = $form->getValue('zeit');
        $thema = $form->getValue('thema');
        $aktermine = new Application_Model_DbTable_Aktermine();
        $aktermine->addTermine( $kopfnr, $dat, $zeit, $thema);

And, my add function in my database class:

public function addTermine($kopfnr, $datum, $zeit, $thema)
    {
        $data = array(
            'kopfnr' => $kopfnr,
            'datum' => $datum,
            'zeit' => $zeit,
            'thema' => $thema,                  
        );
        $this->insert($data);
    }

I´m using a mySQL database on a WAMP installation.

Where is my error? As a remark I want to say, I get a new record, the value of "thema" and the keys are properly saved, so I think it must be some format thing somewhere.

EDIT: I get a new record but the fields date and time are empty. I get no errors

NEW: I added a debug test in my controller to see what comes for the date value, here is the answer:

string '01-04-2016' (length=10) (I put in 01-04-2016)

pia-sophie
  • 505
  • 4
  • 21
  • 1
    Aha, if I try with a fix date in the format that phpmyadmin accepts than it works: 2015-04-15. Is there a easy workaround for this date validator trouble? – pia-sophie Apr 19 '15 at 13:48
  • What is your problem exactly? Do you have an error message, or an excepted result? Please add more information about what doesn't work (in your question, using `edit`.) – mins Apr 19 '15 at 13:53
  • Have you tried changing New Zend_Validate_Date('MM-DD-YYYY') to Zend_Validate_Date('YYYY-MM-DD')? – J A Apr 19 '15 at 16:46
  • Yes I have tried, that works, but I´d like the user to put in another format for example DD-MM-YYYY. – pia-sophie Apr 19 '15 at 16:53

2 Answers2

1

By reading your comments I understand you have two problems. The first one is putting the date into your table and the second is to let the user use the normal date format (dd/mm/yyyy).

First problem is solved when you reformat the user input before putting it into the database.You can add the following line into the $form->isValid($formData) part:

$dat = (new DateTime($form->getValue('datum')))->format('Y-m-d'); 

This oneliner will convert the date from 'dd-mm-yyyy' to 'yyyy-mm-dd'.

Benz
  • 2,317
  • 12
  • 19
  • 1
    I tried two changes, in my formclass I changed the validator this way: ->addValidator(New Zend_Validate_Date('DD-MM-YYYY')); and I changed like your suggestion in the controller. But it doesn´t work. It works only, if I have the validator like mySQL favourizes: YYYY-MM-DD. – pia-sophie Apr 20 '15 at 15:44
  • 1
    Hi @pia-sophie, so the form accepts the DD-MM-YYYY format from the user, but it still is not converted before going into the database? Can you use `print_r` to make a print of the `$data ` object and place this in your question? – Benz Apr 21 '15 at 07:07
0

thanks, after the suggestion to debug I changed the format of my date like this: $test1=date('Y/m/d',strtotime($dat));

And now it works!

pia-sophie
  • 505
  • 4
  • 21