Hermes::parseDate PHP Method

parseDate() public static method

Parses a complete date-time string into a Horde_Date object.
public static parseDate ( string $date ) : Horde_Date
$date string The date-time string to parse.
return Horde_Date The parsed date.
    public static function parseDate($date)
    {
        // strptime() is not available on Windows.
        if (!function_exists('strptime')) {
            return new Horde_Date($date);
        }
        // strptime() is locale dependent, i.e. %p is not always matching
        // AM/PM. Set the locale to C to workaround this, but grab the
        // locale's D_FMT before that.
        $format = Horde_Nls::getLangInfo(D_FMT);
        $old_locale = setlocale(LC_TIME, 0);
        setlocale(LC_TIME, 'C');
        // Try exact format match first.
        $date_arr = strptime($date, $format);
        setlocale(LC_TIME, $old_locale);
        if (!$date_arr) {
            // Try with locale dependent parsing next.
            $date_arr = strptime($date, $format);
            if (!$date_arr) {
                // Try throwing at Horde_Date finally.
                return new Horde_Date($date);
            }
        }
        return new Horde_Date(array('year' => $date_arr['tm_year'] + 1900, 'month' => $date_arr['tm_mon'] + 1, 'mday' => $date_arr['tm_mday'], 'hour' => $date_arr['tm_hour'], 'min' => $date_arr['tm_min'], 'sec' => $date_arr['tm_sec']));
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Populate this slice from a time entry form.
  * Assumes the values are POSTed.
  */
 public function readForm()
 {
     // Required
     $this->_properties['date'] = Hermes::parseDate(Horde_Util::getPost('start_date'));
     $this->_properties['hours'] = Horde_Util::getPost('hours');
     $this->_properties['description'] = Horde_Util::getPost('description');
     $this->_properties['id'] = Horde_Util::getPost('id', 0);
     $this->_properties['billable'] = Horde_Util::getPost('billable') ? 1 : 0;
     // Optional
     $client = Horde_Util::getPost('client');
     $this->_properties['client'] = empty($client) ? '' : $client;
     $this->_properties['type'] = Horde_Util::getPost('type');
     $this->_properties['costobject'] = Horde_Util::getPost('costobject');
     $this->_properties['note'] = Horde_Util::getPost('notes');
     // Admin only
     if ($GLOBALS['registry']->isAdmin(array('permission' => 'hermes:timeadmin')) || $GLOBALS['injector']->getInstance('Horde_Perms')->hasPermission('hermes:review', $GLOBALS['registry']->getAuth(), Horde_Perms::EDIT)) {
         $this->_properties['employee'] = Horde_Util::getPost('employee');
         if (empty($this->_properties['employee'])) {
             $this->_properties['employee'] = $GLOBALS['registry']->getAuth();
         }
     } else {
         $this->_properties['employee'] = $GLOBALS['registry']->getAuth();
     }
 }