Pommo_Helper::timeFromStr PHP Method

timeFromStr() public method

piggy backs on adodb's excellent time library -- supporting a wide range of dates (100AD +)
public timeFromStr ( $str )
    function timeFromStr($str)
    {
        if (!defined('ADODB_DATE_VERSION')) {
            // safely load ADODB date library
            require_once Pommo::$_baseDir . 'lib/adodb-time.inc.php';
        }
        // normalize delimiter
        str_replace('-', '/', $str);
        // Extract Year, Month, and Day from a string like "2007/08/03"
        $a = explode("/", $str);
        // Validate the string
        if (count($a) != 3 || !is_numeric($a[0]) || !is_numeric($a[1]) || !is_numeric($a[2])) {
            return false;
        }
        switch (Pommo::$_dateformat) {
            case 1:
                $year = substr($a[0], 0, 4);
                $month = substr($a[1], 0, 2);
                $day = substr($a[2], 0, 2);
                break;
            case 2:
                $year = substr($a[2], 0, 4);
                $month = substr($a[0], 0, 2);
                $day = substr($a[1], 0, 2);
                break;
            case 3:
                $year = substr($a[2], 0, 4);
                $month = substr($a[1], 0, 2);
                $day = substr($a[0], 0, 2);
                break;
            default:
                Pommo::kill('Unknown date_format', TRUE);
        }
        // Y-M-D validation
        if ($month < 1 || $month > 12) {
            return false;
        }
        if ($day < 1 || $day > 31) {
            return false;
        }
        // correction heuristic for short year @ end of century...
        if (strlen($year) == 2) {
            $year = $year < 50 ? '20' . $year : '19' . $year;
        }
        return adodb_mktime(0, 0, 0, $month, $day, $year);
    }

Usage Example

Beispiel #1
0
 function subscriberData(&$in, $p = array())
 {
     $defaults = array('prune' => true, 'active' => true, 'log' => true, 'ignore' => false, 'ignoreInactive' => true, 'skipReq' => false);
     $p = Pommo_Api::getParams($defaults, $p);
     require_once Pommo::$_baseDir . 'classes/Pommo_Fields.php';
     $logger = Pommo::$_logger;
     $fields = Pommo_Fields::get(array('active' => $p['active']));
     $valid = true;
     foreach ($fields as $id => $field) {
         $inactive = $field['active'] == 'on' ? false : true;
         if (!isset($in[$id]) && $p['skipReq']) {
             continue;
         }
         $in[$id] = @trim($in[$id]);
         if (empty($in[$id])) {
             unset($in[$id]);
             // don't include blank values
             if ($field['required'] == 'on') {
                 if ($p['log']) {
                     $logger->addErr(sprintf(Pommo::_T('%s is a required field.'), $field['prompt']));
                 }
                 $valid = false;
             }
             continue;
         }
         // shorten
         $in[$id] = substr($in[$id], 0, 255);
         switch ($field['type']) {
             case "checkbox":
                 if (strtolower($in[$id]) == 'true') {
                     $in[$id] = 'on';
                 }
                 if (strtolower($in[$id]) == 'false') {
                     $in[$id] = '';
                 }
                 if ($in[$id] != 'on' && $in[$id] != '') {
                     if ($p['ignore'] || $inactive && $p['ignoreInactive']) {
                         unset($in[$id]);
                         break;
                     }
                     if ($p['log']) {
                         $logger->addErr(sprintf(Pommo::_T('Illegal input for field %s.'), $field['prompt']));
                     }
                     $valid = false;
                 }
                 break;
             case "multiple":
                 if (is_array($in[$id])) {
                     foreach ($in[$id] as $key => $val) {
                         if (!in_array($val, $field['array'])) {
                             if ($p['ignore'] || $inactive && $p['ignoreInactive']) {
                                 unset($in[$id]);
                                 break;
                             }
                             if ($p['log']) {
                                 $logger->addErr(sprintf(Pommo::_T('Illegal input for field %s.'), $field['prompt']));
                             }
                             $valid = false;
                         }
                     }
                 } elseif (!in_array($in[$id], $field['array'])) {
                     if ($p['ignore'] || $inactive && $p['ignoreInactive']) {
                         unset($in[$id]);
                         break;
                     }
                     if ($p['log']) {
                         $logger->addErr(sprintf(Pommo::_T('Illegal input for field %s.'), $field['prompt']));
                     }
                     $valid = false;
                 }
                 break;
             case "date":
                 // convert date to timestamp [float; using adodb time library]
                 if (is_numeric($in[$id])) {
                     $in[$id] = Pommo_Helper::timeToStr($in[$id]);
                 }
                 $in[$id] = Pommo_Helper::timeFromStr($in[$id]);
                 if (!$in[$id]) {
                     if ($p['ignore'] || $inactive && $p['ignoreInactive']) {
                         unset($in[$id]);
                         break;
                     }
                     if ($p['log']) {
                         $logger->addErr(sprintf(Pommo::_T('Field (%s) must be a date (' . Pommo_Helper::timeGetFormat() . ').'), $field['prompt']));
                     }
                     $valid = false;
                 }
                 break;
             case "number":
                 if (!is_numeric($in[$id])) {
                     if ($p['ignore'] || $inactive && $p['ignoreInactive']) {
                         unset($in[$id]);
                         break;
                     }
                     if ($p['log']) {
                         $logger->addErr(sprintf(Pommo::_T('Field (%s) must be a number.'), $field['prompt']));
                     }
                     $valid = false;
                 }
                 break;
         }
     }
     // prune
     if ($p['prune']) {
         $in = Pommo_Helper::arrayIntersect($in, $fields);
     }
     return $valid;
 }
All Usage Examples Of Pommo_Helper::timeFromStr