/** * This function parses a date by it's format and returns an * array with the date values. * * @param $date A YDDate object, timestamp, array or string. * If null, the object date. Default: null. * @param $format (Optional) The format name. Default: 'ISO'. * @param $family (Optional) If true, parses in all the format family. Default: true. * @param $error (Optional) If true, errors are triggered. Default: true. * * @returns An array with the date values. * * @static If $date is passed. */ function parse($date = null, $format = 'ISO', $family = true, $error = true) { if ($date === null) { return $this->toArray(); } $old_date = $date; $match = false; $valid_date = true; $valid_time = true; $month_name = null; $result = array(); $result['year'] = $year = 0; $result['month'] = $month = 0; $result['day'] = $day = 0; $result['hours'] = $hours = 0; $result['minutes'] = $minutes = 0; $result['seconds'] = $seconds = 0; if (is_string($date) && $date == '__NOW__') { $date = time(); } if (is_int($date)) { $result['year'] = (int) strftime('%Y', $date); $result['month'] = (int) strftime('%m', $date); $result['day'] = (int) strftime('%d', $date); $result['hours'] = (int) strftime('%H', $date); $result['minutes'] = (int) strftime('%M', $date); $result['seconds'] = (int) strftime('%S', $date); return $result; } else { if (is_object($date) && is_a($date, 'YDDate')) { if ($date->isDateEmpty() && $date->isTimeEmpty()) { return $result; } return $date->toArray(); } else { if (is_array($date)) { if (empty($date)) { return $result; } if (isset($date['day']) && isset($date['month']) && isset($date['year'])) { $day = $date['day']; $month = $date['month']; $year = $date['year']; $match = true; if (isset($date['hours']) && isset($date['minutes']) && isset($date['seconds'])) { $hours = $date['hours']; $minutes = $date['minutes']; $seconds = $date['seconds']; } } } } } if (is_string($date)) { $date = trim($date); if ($date === YDDateFormat::getEmpty($format)) { return $result; } $fam = array(); $fam[] = $format; if ($family) { $formats = YDConfig::get('YD_DATE_FORMATS'); foreach ($formats as $name => $arr) { if (empty($arr['parts'])) { continue; } if (preg_match('/^' . $format . '(.+)/i', $name, $parts)) { $fam[] = $format . $parts[1]; } } } foreach ($fam as $name) { $p = YDDateFormat::getParts($name); $r = YDDateFormat::getRegexes($name); if (preg_match(YDDate::getRegex($name, $r), $date, $date_parts)) { foreach ($p as $var => $num) { ${$var} = isset($date_parts[$num]) ? $date_parts[$num] : 0; } $match = true; if ($month_name) { for ($i = 1; $i <= 12; $i++) { $month_abbr = strtolower(strftime('%b', mktime(0, 0, 0, $i, 1, 2000))); if (substr(strtolower($month_name), 0, 3) == substr($month_abbr, 0, 3)) { $month = $i; break; } } } break; } } } $valid_date = YDDate::isValidDate($day, $month, $year); $valid_time = YDDate::isValidTime($hours, $minutes, $seconds); if (!$match || !$valid_date || !$valid_time) { if ($error) { trigger_error('The date "' . $old_date . '" is not valid.', YD_ERROR); } return false; } $result['year'] = (int) $year; $result['month'] = (int) $month; $result['day'] = (int) $day; $result['hours'] = (int) $hours; $result['minutes'] = (int) $minutes; $result['seconds'] = (int) $seconds; return $result; }