Kronolith::parseDate PHP Method

parseDate() public static method

Parses a complete date-time string into a Horde_Date object.
public static parseDate ( string $date, boolean $withtime = true, $timezone = null ) : Horde_Date
$date string The date-time string to parse.
$withtime boolean Whether time is included in the string.
return Horde_Date The parsed date.
    public static function parseDate($date, $withtime = true, $timezone = null)
    {
        // strptime() is not available on Windows.
        if (!function_exists('strptime')) {
            return new Horde_Date($date, $timezone);
        }
        // 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);
        if ($withtime) {
            $format .= ' ' . ($GLOBALS['prefs']->getValue('twentyFour') ? '%H:%M' : '%I:%M %p');
        }
        $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, $timezone);
            }
        }
        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']), $timezone);
    }

Usage Example

Example #1
0
 /**
  * TODO
  */
 public function saveTask()
 {
     if (!$GLOBALS['registry']->hasMethod('tasks/updateTask') || !$GLOBALS['registry']->hasMethod('tasks/addTask')) {
         return false;
     }
     $id = $this->vars->task_id;
     $list = $this->vars->old_tasklist;
     $task = $this->vars->task;
     $result = $this->_signedResponse('tasklists|tasks/' . $task['tasklist']);
     $due = trim($task['due_date'] . ' ' . $task['due_time']);
     if (!empty($due)) {
         try {
             $due = Kronolith::parseDate($due);
             $task['due'] = $due->timestamp();
         } catch (Exception $e) {
             $GLOBALS['notification']->push($e, 'horde.error');
             return $result;
         }
     }
     if ($task['alarm']['on']) {
         $value = $task['alarm']['value'];
         $unit = $task['alarm']['unit'];
         if ($value == 0) {
             $value = $unit = 1;
         }
         $task['alarm'] = $value * $unit;
         if (isset($task['alarm_methods']) && isset($task['methods'])) {
             foreach (array_keys($task['methods']) as $method) {
                 if (!in_array($method, $task['alarm_methods'])) {
                     unset($task['methods'][$method]);
                 }
             }
             foreach ($task['alarm_methods'] as $method) {
                 if (!isset($task['methods'][$method])) {
                     $task['methods'][$method] = array();
                 }
             }
         } else {
             $task['methods'] = array();
         }
     } else {
         $task['alarm'] = 0;
         $task['methods'] = array();
     }
     unset($task['alarm_methods']);
     if (!isset($task['completed'])) {
         $task['completed'] = false;
     }
     if ($this->vars->recur && !empty($due)) {
         $task['recurrence'] = Kronolith_Event::readRecurrenceForm($due, 'UTC');
     }
     $task['tags'] = Horde_Util::getFormData('tags');
     try {
         $ids = $id && $list ? $GLOBALS['registry']->tasks->updateTask($list, $id, $task) : $GLOBALS['registry']->tasks->addTask($task);
         if (!$id) {
             $id = $ids[0];
         }
         $task = $GLOBALS['registry']->tasks->getTask($task['tasklist'], $id);
         $result->tasks = array($id => $task->toJson(false, $GLOBALS['prefs']->getValue('twentyFour') ? 'H:i' : 'h:i A'));
         $result->type = $task->completed ? 'complete' : 'incomplete';
         $result->list = $task->tasklist;
     } catch (Exception $e) {
         $GLOBALS['notification']->push($e, 'horde.error');
         return $result;
     }
     if ($due && ($kronolith_driver = $this->_getDriver('tasklists|tasks/' . $task->tasklist))) {
         try {
             $event = $kronolith_driver->getEvent('_tasks' . $id);
             $end = clone $due;
             $end->hour = 23;
             $end->min = $end->sec = 59;
             $start = clone $due;
             $start->hour = $start->min = $start->sec = 0;
             $events = array();
             Kronolith::addEvents($events, $event, $start, $end, true, true);
             if (count($events)) {
                 $result->events = $events;
             }
         } catch (Horde_Exception_NotFound $e) {
         } catch (Exception $e) {
             $GLOBALS['notification']->push($e, 'horde.error');
         }
     }
     return $result;
 }
All Usage Examples Of Kronolith::parseDate