public function render($attributes = array()) { $this->setAttributes($attributes); if ($this->value instanceof DateTime) { $this->origValue = $this->value; $this->value = $this->value->format($this->format); } elseif ($this->value) { // reformat time $date = new DateTime("2012-01-01 " . $this->value); $this->value = $date->format($this->format); } return parent::render(); }
public function render($attributes = array()) { $this->setAttributes($attributes); $selfId = $this->getSerialId(); $this->setId($selfId); // @var array[string]string formatIds array contains the format charactor and corresponding // element ID. $formatIds = array(); $nodes = array(); for ($i = 0; $i < strlen($this->format); $i++) { $c = $this->format[$i]; if (isset($this->formatOptions[$c])) { $value = null; if ($this->dateObject) { $value = $this->dateObject->format($c); } else { if ($this->value) { $value = $this->value; } } $select = new SelectInput(array('options' => $this->formatOptions[$c], 'value' => $value, 'allow_empty' => $this->allow_empty, 'class' => '+=form-control')); $id = $select->getSerialId(); // $select->setId($id); $formatIds[$c] = $id; $nodes[] = $select; } else { $nodes[] = $c; } } $ids = array_values($formatIds); ob_start(); ?> <script> // when changing selector values // update date string by the format $(function() { var s = document.getElementById('<?php echo $selfId; ?> '); var columns = <?php echo json_encode($formatIds); ?> ; var format = '<?php echo $this->storageFormat; ?> '; function updater() { var datestr = ''; for ( var i = 0; i < format.length ; i++ ) { var c = format[i]; var sid = columns[c]; if (sid) { datestr += document.getElementById(sid).value; } else { datestr += c; } } // format date string <?php if ($this->convert_format) { ?> // parse datestring var d = new Date(datestr); // get timestamp and add timezone // <?php // d = new Date( d.getTime() + <?=$this->timezone->getOffset( $this->dateObject ) ?> ); ?> // use built-in date formatter s.value = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + (d.getDate()); if( d.getHours() && d.getMinutes() ) s.value += ' ' + d.getHours() + ':' + d.getMinutes(); <?php } else { ?> s.value = datestr; <?php } ?> // console.log('debug date', s.value); } for (var c in columns) { var id = columns[c]; if( typeof jQuery !== 'undefined' ) { $('#' + id).bind('change',updater); } else { document.getElementById(id).addEventListener('change',updater,false); } } }); </script> <?php $script = ob_get_contents(); ob_end_clean(); return '<div class="form-inline">' . parent::render() . $this->_renderNodes($nodes) . $script . '</div>'; }