开发者

zend locale is making me nervous: wrong datepresentation

开发者 https://www.devze.com 2022-12-15 16:50 出处:网络
First I had problems with getting date\'s into my db see here. Now I have problems with getting it into the database on the right format.

First I had problems with getting date's into my db see here. Now I have problems with getting it into the database on the right format. This is my code:

public function editAction()
    {
        if($this->_request->getParam('id')){
            $id = $this->_request->getParam('id');
            $data = $this->_evtObj->selectOneRow($id);
            // initialize the form
            //var_dump($data);
            $form = new JS_Form_EventForm();

            $array = $data->toArray(); 
            //$locale = Zend_Registry::get('locale');
            $locale = new Zend_Locale();
            $date1 = new Zend_Date($locale);
            $date1->set($array[0]['evt_startdate']);
            $array[0]['evt_startdate'] = $date1->get();
            $array[0]['evt_enddate'] = date('%d-%m-%Y',(string)$array[0]['evt_enddate']);
            $form->开发者_JAVA技巧;populate($array[0]);
            $this->view->form =$form;
        }

As you can see it populates the form with dates from the db. In the db the date is saved like 2010-01-15. As you can see in the above example i tried out two things:

locale = new Zend_Locale();
$date1 = new Zend_Date($locale);
$date1->set($array[0]['evt_startdate']);
$array[0]['evt_startdate'] = $date1->get();

This shows the date like: '1262300400'

and:

 $array[0]['evt_enddate'] = date('%d-%m-%Y',(string)$array[0]['evt_enddate']);

this shows the date like: '%01-%01-%1970'

I want the date to be shown like dd-mm-yyyy

How to deal with this? All this date business drives me mad.

I am running zf 1.9.6

any idea's?


I'd suggest using something like this

$date = new Zend_Date($array[0]['evt_startdate'],Zend_Date::DATES);
$array[0]['evt_startdate'] = $date1->toString('dd-MM-YYYY');

Documentation


Try date('d-m-Y', $array[0]['evt_enddate']) Also, the second param to date should be a timestamp. I do not know what is inside that array you use, but it certainly does not need to be typecasted to string. If you want to create timestamp from a formatted date, use strftime().

If you use get() without any arguments, the timestamp is returned. If you want to get the date part with time from Zend_Date use getDate() instead of get(). That will set the time part to 00:00:00 and return it with the date. If you only want the date part use get() with the DATE_MEDIUM constant. If you just want to format from Zend_Date, use Peter Lindqvist's suggested answer below.

Opinion: Personally I'd suggest to avoid Zend_Date altogether. I have the impression it is akward to use and pretty slow compared to PHP's native DateTime and DateTimezone classes. I use Zend_Date only for localized formatting nowadays, because that's where it shines.

0

精彩评论

暂无评论...
验证码 换一张
取 消