/** * @param string $time erstellt am Namen den Monat * * @return bool */ public function setMonth($time) { $date = date_create($time); if ($date == false) { throw new Exception('Date string is wrong: ' . $time . ' - ' . json_encode(date_get_last_errors())); } $this->setDate($date); }
/** * Ensures that the validator can handle different manual dateformats * * @group ZF-2003 * @return void */ public function testUseManualFormat() { $this->assertTrue($this->validator->setFormat('d.m.Y')->isValid('10.01.2008'), var_export(date_get_last_errors(), 1)); $this->assertEquals('d.m.Y', $this->validator->getFormat()); $this->assertTrue($this->validator->setFormat('m Y')->isValid('01 2010')); $this->assertFalse($this->validator->setFormat('d/m/Y')->isValid('2008/10/22')); $this->assertTrue($this->validator->setFormat('d/m/Y')->isValid('22/10/08')); $this->assertFalse($this->validator->setFormat('d/m/Y')->isValid('22/10')); // Omitting the following assertion, as it varies from 5.3.3 to 5.3.11, // and there is no indication in the PHP changelog as to when or why it // may have changed. Leaving for posterity, to indicate original expectation. // $this->assertFalse($this->validator->setFormat('s')->isValid(0)); }
function generateLabels(&$dob, &$off) { $parts = explode('-', $dob); $date = @date_create("{$parts[2]}-{$parts[0]}-{$parts[1]}"); if (!$date) { $e = date_get_last_errors(); foreach ($e['errors'] as $error) { echo "{$error}\n"; } exit(1); } date_add($date, date_interval_create_from_date_string($off . ' days')); date_add($date, date_interval_create_from_date_string('-10 days')); $labels = '0:'; for ($i = 0; $i <= 10; ++$i) { $labels .= '|' . date_format($date, 'M d'); date_add($date, date_interval_create_from_date_string('2 days')); } return $labels; }
<?php $dt = date_create("asdfasdf"); $errs = date_get_last_errors(); var_dump(count($errs) === 4); var_dump($errs['warning_count'] === 1); $err_warnings = $errs['warnings']; var_dump(count($err_warnings) === 1); var_dump($err_warnings[6] === "Double timezone specification"); var_dump($errs['error_count'] === 1); $err_errors = $errs['errors']; var_dump(count($err_errors) === 1); var_dump($err_errors[0] === "The timezone could not be found in the database");
<?php var_dump(date_get_last_errors()); // no date was parsed, so no errors
<?php $tz = new DateTimeZone("UTC"); $date = "06/08/04 12:00"; echo "==\n"; print_r(date_create_from_format('m/d/y', $date, $tz)); print_r(date_get_last_errors()); echo "==\n"; print_r(date_create_from_format('m/d/y+', $date, $tz)->setTime(0, 0)); print_r(date_get_last_errors()); echo "==\n"; print_r(date_create_from_format('+m/d/y', $date, $tz)->setTime(0, 0)); print_r(date_get_last_errors()); echo "==\n"; print_r(date_create_from_format('m/d/y++', $date, $tz)->setTime(0, 0)); print_r(date_get_last_errors()); echo "==\n"; $date = "06/08/04"; print_r(date_create_from_format('m/d/y+', $date, $tz)->setTime(0, 0)); print_r(date_get_last_errors()); echo "==\n"; print_r(date_create_from_format('+m/d/y', $date, $tz)->setTime(0, 0)); print_r(date_get_last_errors()); echo "==\n";
/** * Create new ActiveDateTime from string, using the connection's settings * @param string $str String representation of a datetime * @return ActiveDateTime ActiveDateTime instance */ public function stringToDateTime($str) { // Empty date? if (empty($str) || $str == '0000-00-00 00:00:00') { return null; } // Create date $date = date_create($str); // Any errors? $errors = date_get_last_errors(); if ($errors['warning_count'] > 0 || $errors['error_count'] > 0) { $messages = array_merge($errors['warnings'], $errors['errors']); throw new \Exception("Could not convert '{$str}' to DateTime: " . implode('. ', $messages), 1); return null; } // Create activeDT return new ActiveDateTime($date->format('Y-m-d H:i:s T')); }
public static function getLastErrors() { return date_get_last_errors(); }
protected function parseDate($value, $format = null) { if ($value instanceof \DateTime) { return $value->getTimestamp(); } if ($format === null) { return is_string($value) ? strtotime($value) : (is_int($value) ? $value : false); } $formats = ['c' => 'Y-m-d\\TH:i:sP', 'r' => 'D, d M Y H:i:s O']; if (isset($formats[$format])) { $format = $formats[$format]; } $value = date_create_from_format($format, (string) $value); $debug = date_get_last_errors(); if ($value === false || $debug['warning_count'] !== 0 || $debug['error_count'] !== 0) { return false; } return $value->getTimestamp(); }
} else { array_push($warn_array, 'The password strength is ' . $password_strength . '. Minimum required is ' . MIN_PASSWORD_STRENGTH . '.'); } } // Be sure the membership_date is valid $membership_date = date_create_from_format('Y-m-d', $_POST['membership_date']); $membership_date_errors = date_get_last_errors(); if ($membership_date_errors['warning_count'] != 0 || $membership_date_errors['error_count'] != 0) { array_push($error_array, 'The membership date is invalid or improperly formatted as "YYYY-MM-DD".'); $error['membership_date'] = ' error'; } else { $_POST['membership_date'] = date_format($membership_date, 'Y-m-d'); } // // Be sure the last_renewal_date is valid $last_renewal_date = date_create_from_format('Y-m-d', $_POST['last_renewal_date']); $last_renewal_date_errors = date_get_last_errors(); if ($last_renewal_date_errors['warning_count'] != 0 || $last_renewal_date_errors['error_count'] != 0) { array_push($error_array, 'The last_renewal date is invalid or improperly formatted as "YYYY-MM-DD".'); $error['last_renewal_date'] = ' error'; } else { $_POST['last_renewal_date'] = date_format($last_renewal_date, 'Y-m-d'); } // Be sure the customer fee is a number if (!preg_match('|^(-)?[0-9]*(\\.)?[0-9]*$|', $_POST['customer_fee_percent'])) { array_push($error_array, 'The customer fee must be a postive or negative decimal number.'); $error['customer_fee_percent'] = ' error'; } } // If there are no validation errors, then post the new data and get the changed values from the database if (count($error_array) == 0 && ($_REQUEST['action'] == 'Update' || $_REQUEST['action'] == 'Add New')) { // Close the modal dialog and reload the parent when completed.
/** * Converts a date coming from a request param and converts it to a \DateTime * @param string $date * @return [\DateTime, string] * @throws BadArgumentException when the date is invalid or not supplied in the right format */ private function parseRequestDate($date) { $timezone = new \DateTimeZone("UTC"); $granularity = null; if (preg_match('#^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$#', $date)) { $parsedDate = date_create_from_format('Y-m-d\\TH:i:s\\Z', $date, $timezone); $granularity = Identity::GRANULARITY_YYYY_MM_DDTHH_MM_SSZ; } elseif (preg_match('#^\\d{4}-\\d{2}-\\d{2}$#', $date)) { // Add ! to format to set time to 00:00:00 $parsedDate = date_create_from_format('!Y-m-d', $date, $timezone); $granularity = Identity::GRANULARITY_YYYY_MM_DD; } else { throw new BadArgumentException("Expected a data in one of the following formats: " . Identity::GRANULARITY_YYYY_MM_DDTHH_MM_SSZ . " OR " . Identity::GRANULARITY_YYYY_MM_DD . " FOUND " . $date); } $parseResult = date_get_last_errors(); if (!$parsedDate || $parseResult['error_count'] > 0 || $parseResult['warning_count'] > 0) { throw new BadArgumentException("{$date} is not a valid date"); } return [$parsedDate, $granularity]; }
/** * parses a date string into UNIX timestamp * * if "strict dates" is set, this function uses the DateTime or IntlDateFormatter * class to parse the string according to a specific format. If it is not, we * use the conventional strtotime() function, with the enhancement that if the * non-American style format is used with slashes "d/m/Y" the string is prepared * so strtotime can parse it correctly * * @param string $string the string to parse; if not given, defaults to now * @param object $column_atts the column object; used to identify the field for * user feedback * @param bool $zero_time if set, zero the time portion of the date so it * won't interfere with date comparisons * @return int|bool UNIX timestamp or false if parse fails */ public static function parse_date($string = false, $column = '', $zero_time = false) { if (false === $string) { return false; } $string = Participants_Db::set_filter('parse_date', $string, $column); // it's already a timestamp if (self::is_valid_timestamp($string)) { //if (WP_DEBUG and is_object($column)) error_log(__METHOD__.' tried to parse timestamp from '. $column->name); return $string; } $date = false; // if it is a default zero timestamp or other empty value, treat it as "no date" if ($string == '0000-00-00 00:00:00' || empty($string)) { return false; } /* * we have two options to parse a date string into a timestamp: the * IntlDateFormatter class or the DateTime class. The IntlDateFormatter * class can parse localized text dates, but it seems commonly unavailable, * at least on English-speaking servers. The DateTime class is widely * available, but can't parse non-English text dates. It can parse numeric * date representations, so if the intl module is not available, we try to * use DateTime. If that is not available, we use strtotime with the added trick * of swapping out the separators if they are slashes so slashed European * notation can be correctly parsed */ $mode = 'none'; $timestamp = is_object($column) && $column->form_element == 'timestamp' || preg_match('#^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$#', $string) == 1 ? true : false; if (self::$plugin_options['strict_dates'] == 1 and is_object($column) and !$timestamp) { if (class_exists('IntlDateFormatter')) { $mode = 'Intl'; $DateFormat = new IntlDateFormatter(WPLANG, IntlDateFormatter::LONG, IntlDateFormatter::NONE, NULL, NULL, Participants_Db::get_ICU_date_format(self::$date_format)); //error_log(__METHOD__.' format object:'.print_r($DateFormat,1)); $timestamp = $DateFormat->parse($string); $the_Date = new DateTime(); $the_Date->setTimestamp($timestamp); } else { if (class_exists('DateTime')) { $mode = 'DateTime'; $the_Date = DateTime::createFromFormat(self::$date_format, $string); } } //error_log(__METHOD__.' date:'.print_r($the_Date,1)); if (is_array(date_get_last_errors()) && !empty($string)) { $errors = date_get_last_errors(); if ($errors['warning_count'] > 0 || $errors['error_count'] > 0) { $the_Date = false; if (is_object(self::$validation_errors) and is_object($column)) { self::$validation_errors->add_error($column->name, sprintf(__('The date for "%s" was invalid. Please input the date with the exact format shown', 'participants-database'), $column->title)); } } } /* * if we have a valid date, convert to timestamp */ if ($the_Date) { /* * zero the time so date equality comparisons can be made */ if ($zero_time) { $the_Date->setTime(0, 0); } $date = $the_Date->format('U'); } } // ob_start(); // var_dump($date); // error_log(__METHOD__.' date value:'.ob_get_clean().' mode:'.$mode); /* * if we haven't got a timestamp, parse the date the regular way */ if ($date === false or !self::is_valid_timestamp($date)) { $mode = 'strtotime'; if (is_object($column) && $column->form_element == 'date') { /* * deal with the common special case of non-American-style numeric date with slashes */ if (false !== strpos($string, '/')) { $date_parts = explode('/', self::$date_format); $day_index = array_search('d', $date_parts) !== false ? array_search('d', $date_parts) : array_search('j', $date_parts); $month_index = array_search('m', $date_parts) !== false ? array_search('m', $date_parts) : array_search('n', $date_parts); if ($day_index !== false && $month_index !== false && $day_index < $month_index) { $string = str_replace('/', '-', $string); } } } elseif (is_object($column) && $column->form_element == 'timestamp') { if ($zero_time) { /* * we need to zero the time, we first try to do it using the DateTime class */ $the_Date = new DateTime($string); if (is_object($the_Date)) { $the_Date->setTime(0, 0); $string = $the_Date->format(self::$date_format); } else { /* * remove the time portion of the timestamp */ $string = preg_replace('# [0-9]{2}:[0-9]{2}:[0-9]{2}$#', '', $string); $string .= ' 00:00 -0'; } } } /* * Most of the time, the default PHP timezone is the current setting, but * experience has shown it's necessary to reset it for the conversion to make * sure. We also must assume that the database server and PHP server are on * the same TZ. */ date_default_timezone_set(ini_get('date.timezone')); // ini_get('date.timezone') $date = strtotime($string); } //if (WP_DEBUG) error_log(__METHOD__.' mode: ' . $mode . ' timestamp:' . $date); return $date; }