本文整理汇总了PHP中DateTime::getLastErrors方法的典型用法代码示例。如果您正苦于以下问题:PHP DateTime::getLastErrors方法的具体用法?PHP DateTime::getLastErrors怎么用?PHP DateTime::getLastErrors使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTime
的用法示例。
在下文中一共展示了DateTime::getLastErrors方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validate
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof DateTime) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\DateTime');
}
if (null === $value || '' === $value || $value instanceof \DateTime) {
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
$value = (string) $value;
\DateTime::createFromFormat($constraint->format, $value);
$errors = \DateTime::getLastErrors();
if (0 < $errors['error_count']) {
$this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(DateTime::INVALID_FORMAT_ERROR)->addViolation();
return;
}
foreach ($errors['warnings'] as $warning) {
if ('The parsed date was invalid' === $warning) {
$this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(DateTime::INVALID_DATE_ERROR)->addViolation();
} elseif ('The parsed time was invalid' === $warning) {
$this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(DateTime::INVALID_TIME_ERROR)->addViolation();
} else {
$this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->setCode(DateTime::INVALID_FORMAT_ERROR)->addViolation();
}
}
}
示例2: transform
/**
* Transforms a date string in the configured timezone into a DateTime object.
*
* @param string $value A value as produced by PHP's date() function
*
* @throws TransformationFailedException If the given value is not a string,
* if the date could not be parsed or
* if the input timezone is not supported
*
* @return \DateTime|null An instance of \DateTime
*/
public function transform($value)
{
if (empty($value)) {
return;
}
if (!is_string($value)) {
throw new TransformationFailedException('Expected a string.');
}
try {
$outputTz = new \DateTimeZone($this->outputTimezone);
$dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz);
$lastErrors = \DateTime::getLastErrors();
if (0 < $lastErrors['warning_count'] || 0 < $lastErrors['error_count']) {
throw new TransformationFailedException(implode(', ', array_merge(array_values($lastErrors['warnings']), array_values($lastErrors['errors']))));
}
if ($this->inputTimezone !== $this->outputTimezone) {
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
}
} catch (TransformationFailedException $e) {
throw $e;
} catch (\Exception $e) {
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
}
return $dateTime;
}
示例3: __construct
public function __construct($datetime, $use_raw_pattern = false, $strict = false)
{
if ($use_raw_pattern === false) {
$pattern = OSCOM::getDef('date_time_format');
} else {
$pattern = $this->raw_pattern_date . ' ' . $this->raw_pattern_time;
}
// format time as 00:00:00 if it is missing from the date
$new_datetime = strtotime($datetime);
if ($new_datetime !== false) {
$new_datetime = date($pattern, $new_datetime);
$this->datetime = \DateTime::createFromFormat($pattern, $new_datetime);
$strict_log = false;
}
if ($this->datetime === false) {
$strict_log = true;
} else {
$errors = \DateTime::getLastErrors();
if ($errors['warning_count'] > 0 || $errors['error_count'] > 0) {
$this->datetime = false;
$strict_log = true;
}
}
if ($strict === true && $strict_log === true) {
trigger_error('DateTime: ' . $datetime . ' (' . $new_datetime . ') cannot be formatted to ' . $pattern);
}
}
示例4: transform
/**
* @inheritdoc
*/
public function transform($value)
{
if (null === $value) {
return null;
}
if (is_scalar($value)) {
$value = (string) $value;
}
if (!is_string($value)) {
throw new TransformationFailedException(sprintf('Expected a string to transform, got "%s" instead.', json_encode($value)));
}
if ('' === $value) {
return null;
}
try {
$outputTz = new \DateTimeZone($this->outputTimezone);
$dateTime = \DateTime::createFromFormat($this->format, $value, $outputTz);
$lastErrors = \DateTime::getLastErrors();
if (0 < $lastErrors['warning_count'] || 0 < $lastErrors['error_count']) {
throw new TransformationFailedException(implode(', ', array_merge(array_values($lastErrors['warnings']), array_values($lastErrors['errors']))));
}
} catch (\Exception $e) {
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
}
return $dateTime;
}
示例5: validate
/**
* Filter & validate recipients
*/
public function validate()
{
parent::validate();
$recipients = explode("\n", str_replace("\r", '', $this->getData('recipients')));
$recipients = array_unique(array_map('trim', $recipients));
$recipients = array_filter($recipients, 'strlen');
if (false === $recipients || count($recipients) < 1) {
$this->addError('Recipients cannot be empty');
}
try {
$startDate = new DateTime($this->getData('start_date'));
} catch (Exception $e) {
$this->addError('Invalid Start Date');
return !$this->hasErrors();
}
// Get last errors & warnings
$errors = DateTime::getLastErrors();
if (!empty($errors['errors']) || !empty($errors['warnings'])) {
$this->addError('Invalid Start Date');
}
// Set filtered & validated state
$this->setData('recipients', $recipients);
$this->setData('start_date', $startDate->format(Varien_Date::DATETIME_PHP_FORMAT));
return !$this->hasErrors();
}
示例6: parseDateValue
public function parseDateValue($value)
{
$res = false;
if (preg_match('/^\\d\\d\\d\\d-\\d\\d-\\d\\d( \\d\\d:\\d\\d(:\\d\\d){0,1}){0,1}$/', $value, $matches)) {
switch (count($matches)) {
case 1:
if (!$this->time_required) {
$res = DateTime::createFromFormat('Y-m-d H:i:s', $value . ' 00:00:00');
}
break;
case 2:
$res = DateTime::createFromFormat('Y-m-d H:i:s', $value . ':00');
break;
case 3:
$res = DateTime::createFromFormat('Y-m-d H:i:s', $value);
break;
default:
$res = false;
}
}
// check there were no warnings because of invalid date values (strict checking)
if ($res) {
$errs = DateTime::getLastErrors();
if (!empty($errs['warning_count'])) {
$res = false;
}
}
return $res;
}
示例7: run
public function run($value)
{
\DateTime::createFromFormat($this->format, $value);
$errors = \DateTime::getLastErrors();
if (count($errors['warnings']) || count($errors['errors'])) {
$this->addError('must_be_datetime_format', $this->format);
}
}
示例8: testCreateFromFormatFailure
public function testCreateFromFormatFailure()
{
$time = 'foo';
$immutable = DateTimeImmutable::createFromFormat(DateTime::RFC3339, $time);
$mutable = DateTime::createFromFormat(DateTime::RFC3339, $time);
$this->assertFalse($immutable);
$this->assertSame(DateTime::getLastErrors(), DateTimeImmutable::getLastErrors());
}
示例9: dateTimeIsValid
/**
* Overcoming a PHP \DateTime poor design choice.
*
* @param \DateTime $dateTime
* @return bool
*/
public static function dateTimeIsValid(\DateTime $dateTime)
{
try {
\DateTime::createFromFormat(\DateTime::ISO8601, $dateTime->format(\DateTime::ISO8601));
} catch (\Exception $e) {
return false;
}
return \DateTime::getLastErrors()['warning_count'] == 0 && \DateTime::getLastErrors()['error_count'] == 0;
}
示例10: getDate
public function getDate($value, $format)
{
$date = \DateTime::createFromFormat($format, $value);
$lastRes = \DateTime::getLastErrors();
if ($lastRes['warning_count'] != 0 || $lastRes['error_count'] != 0) {
return false;
}
return $date;
}
示例11: datef
/**
* This function helps with Date
* @param array $columns represents an array of columns.
* foreach $column in columns = array( 'heading' => 'Name Of Column', 'rows' => array('id' => 'data'), 'action' => false || 'action' = array('Controller'=>'action') );
*/
public function datef($date, $error_string = 'Invalid Date', $format = DATE_STANDARD)
{
$temp = new DateTime($date);
$error = DateTime::getLastErrors();
if (empty($date) || $error['warning_count'] > 0 || $error['error_count'] > 0) {
return __($error_string);
}
return $temp->format($format);
}
示例12: isValidDateTimeString
/**
* Check if a string is a valid date(time)
*
* @link http://www.pontikis.net/tip/?id=21
*
* @param string $dateStr
* @param string $dateFormat
* @param string $timezone (If timezone is invalid, php will throw an exception)
* @return bool
*/
function isValidDateTimeString($dateStr, $dateFormat, $timezone = null)
{
if ($timezone) {
$date = \DateTime::createFromFormat($dateFormat, $dateStr, new \DateTimeZone($timezone));
} else {
$date = \DateTime::createFromFormat($dateFormat, $dateStr);
}
return $date && \DateTime::getLastErrors()["warning_count"] == 0 && \DateTime::getLastErrors()["error_count"] == 0;
}
示例13: string_to_datetime
public function string_to_datetime($string)
{
$date = date_create(str_replace('.000000', '', $string));
$errors = \DateTime::getLastErrors();
if ($errors['warning_count'] > 0 || $errors['error_count'] > 0) {
return null;
}
return $date;
}
示例14: createFromFormat
/**
* Returns new DateTime object formatted according to the specified format
*
* @param string $format The format that the passed in string should be in
* @param string $time String representing the time
* @return \DateTime
* @throws Exception\InvalidDateStructureException If creation fail
*/
public static function createFromFormat($format, $time)
{
if ($dateTime = \DateTime::createFromFormat($format, $time)) {
return $dateTime;
}
$errors = \DateTime::getLastErrors();
$msg = trim(implode(', ', array_merge($errors['errors'], $errors['warnings'])));
throw new Exception\InvalidDateStructureException($msg);
}
示例15: createDateTime
public static function createDateTime($format, $stringDateTime)
{
$dateTime = \DateTime::createFromFormat($format, $stringDateTime);
$dateErrors = \DateTime::getLastErrors();
if ($dateErrors['warning_count'] + $dateErrors['error_count'] > 0) {
return false;
}
return $dateTime;
}