Beispiel #1
0
 /**
  * DB_DataObject_FormBuilder::_array2date()
  *
  * Takes a date array as used by the QuickForm date element and turns it back into
  * a string representation suitable for use with a database date field (format 'YYYY-MM-DD').
  * If second parameter is true, it will return a unix timestamp instead. //FRANK: Not at this point it wont
  *
  * Beware: For the date conversion to work, you must at least use the letters "d", "m" and "Y" in
  * your format string (see "dateElementFormat" option). If you want to enter a time as well,
  * you will have to use "H", "i" and "s" as well. Other letters will not work! Exception: You can
  * also use "M" instead of "m" if you want plain text month names.
  *
  * @param array $date   An array representation of a date, as user in HTML_QuickForm's date element
  * @param boolean $timestamp  Optional. If true, return a timestamp instead of a string. Defaults to false.
  * @return mixed
  * @access protected
  */
 function _array2date($dateInput, $timestamp = false)
 {
     if (isset($dateInput['M'])) {
         $month = $dateInput['M'];
     } elseif (isset($dateInput['m'])) {
         $month = $dateInput['m'];
     } elseif (isset($dateInput['F'])) {
         $month = $dateInput['F'];
     }
     if (isset($dateInput['Y'])) {
         $year = $dateInput['Y'];
     } elseif (isset($dateInput['y'])) {
         $year = $dateInput['y'];
     }
     if (isset($dateInput['H'])) {
         $hour = $dateInput['H'];
     } elseif (isset($dateInput['h']) || isset($dateInput['g'])) {
         if (isset($dateInput['h'])) {
             $hour = $dateInput['h'];
         } elseif (isset($dateInput['g'])) {
             $hour = $dateInput['g'];
         }
         if (isset($dateInput['a'])) {
             $ampm = $dateInput['a'];
         } elseif (isset($dateInput['A'])) {
             $ampm = $dateInput['A'];
         }
         if (strtolower(preg_replace('/[\\.\\s,]/', '', $ampm)) == 'pm') {
             if ($hour != '12') {
                 $hour += 12;
                 if ($hour == 24) {
                     $hour = '';
                     ++$dateInput['d'];
                 }
             }
         } else {
             if ($hour == '12') {
                 $hour = '00';
             }
         }
     }
     $strDate = '';
     if (isset($year) || isset($month) || isset($dateInput['d'])) {
         if (isset($year) && ($len = strlen($year)) > 0) {
             if ($len < 2) {
                 $year = '0' . $year;
             }
             if ($len < 4) {
                 $year = substr(date('Y'), 0, 2) . $year;
             }
         } else {
             $year = '0000';
         }
         if (isset($month) && ($len = strlen($month)) > 0) {
             if ($len < 2) {
                 $month = '0' . $month;
             }
         } else {
             $month = '00';
         }
         if (isset($dateInput['d']) && ($len = strlen($dateInput['d'])) > 0) {
             if ($len < 2) {
                 $dateInput['d'] = '0' . $dateInput['d'];
             }
         } else {
             $dateInput['d'] = '00';
         }
         $strDate .= $year . '-' . $month . '-' . $dateInput['d'];
     }
     if (isset($hour) || isset($dateInput['i']) || isset($dateInput['s'])) {
         if (isset($hour) && ($len = strlen($hour)) > 0) {
             if ($len < 2) {
                 $hour = '0' . $hour;
             }
         } else {
             $hour = '00';
         }
         if (isset($dateInput['i']) && ($len = strlen($dateInput['i'])) > 0) {
             if ($len < 2) {
                 $dateInput['i'] = '0' . $dateInput['i'];
             }
         } else {
             $dateInput['i'] = '00';
         }
         if (!empty($strDate)) {
             $strDate .= ' ';
         }
         $strDate .= $hour . ':' . $dateInput['i'];
         if (isset($dateInput['s']) && ($len = strlen($dateInput['s'])) > 0) {
             $strDate .= ':' . ($len < 2 ? '0' : '') . $dateInput['s'];
         }
     }
     DB_DataObject_FormBuilder::debug('<i>_array2date():</i>' . serialize($dateInput) . ' to ' . $strDate . ' ...');
     return $strDate;
 }