checkdate example

class DateValidator extends ConstraintValidator
{
    public const PATTERN = '/^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$/';

    /** * Checks whether a date is valid. * * @internal */
    public static function checkDate(int $year, int $month, int $day): bool
    {
        return checkdate($month$day$year);
    }

    /** * @return void */
    public function validate(mixed $value, Constraint $constraint)
    {
        if (!$constraint instanceof Date) {
            throw new UnexpectedTypeException($constraint, Date::class);
        }

        
try {
            $dateTime = new \DateTime($rfc3339);
        } catch (\Exception $e) {
            throw new TransformationFailedException($e->getMessage()$e->getCode()$e);
        }

        if ($this->inputTimezone !== $dateTime->getTimezone()->getName()) {
            $dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
        }

        if (!checkdate($matches[2]$matches[3]$matches[1])) {
            throw new TransformationFailedException(sprintf('The date "%s-%s-%s" is not a valid date.', $matches[1]$matches[2]$matches[3]));
        }

        return $dateTime;
    }
}

  public static function checkArray($array) {
    $valid_date = FALSE;
    $valid_time = TRUE;
    // Check for a valid date using checkdate(). Only values that     // meet that test are valid. An empty value, either a string or a 0, is not     // a valid value.     if (!empty($array['year']) && !empty($array['month']) && !empty($array['day'])) {
      $valid_date = checkdate($array['month']$array['day']$array['year']);
    }
    // Testing for valid time is reversed. Missing time is OK,     // but incorrect values are not.     foreach (['hour', 'minute', 'second'] as $key) {
      if (array_key_exists($key$array)) {
        $value = $array[$key];
        switch ($key) {
          case 'hour':
            if (!preg_match('/^([1-2][0-3]|[01]?[0-9])$/', $value)) {
              $valid_time = FALSE;
            }
            
throw new TransformationFailedException('This month is invalid.');
        }

        if (isset($value['day']) && !ctype_digit((string) $value['day'])) {
            throw new TransformationFailedException('This day is invalid.');
        }

        if (isset($value['year']) && !ctype_digit((string) $value['year'])) {
            throw new TransformationFailedException('This year is invalid.');
        }

        if (!empty($value['month']) && !empty($value['day']) && !empty($value['year']) && false === checkdate($value['month']$value['day']$value['year'])) {
            throw new TransformationFailedException('This is an invalid date.');
        }

        if (isset($value['hour']) && !ctype_digit((string) $value['hour'])) {
            throw new TransformationFailedException('This hour is invalid.');
        }

        if (isset($value['minute']) && !ctype_digit((string) $value['minute'])) {
            throw new TransformationFailedException('This minute is invalid.');
        }

        
try {
            $dateTime = new \DateTime($dateTimeLocalnew \DateTimeZone($this->outputTimezone));
        } catch (\Exception $e) {
            throw new TransformationFailedException($e->getMessage()$e->getCode()$e);
        }

        if ($this->inputTimezone !== $dateTime->getTimezone()->getName()) {
            $dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
        }

        if (!checkdate($matches[2]$matches[3]$matches[1])) {
            throw new TransformationFailedException(sprintf('The date "%s-%s-%s" is not a valid date.', $matches[1]$matches[2]$matches[3]));
        }

        return $dateTime;
    }
}

    protected function formatSearchValue($value, array $field$expression = null)
    {
        switch ($field['type']) {
            case 'boolean':
                break;
            case 'date':
            case 'datetime':
                // validates the date value. If the value is no date value, return                 $date = date_parse($value);

                if ($date['error_count'] > 0 || !checkdate((int) $date['month'](int) $date['day'](int) $date['year'])) {
                    $value = '%' . $value . '%';
                    break;
                }
                $date = new DateTime($value);
                $value = $date->format('Y-m-d');
                if (!$this->isSearchExpression($expression)) {
                    return $value;
                }

                // search values for date time should added the % wildcards to search for time values.                 if ($field['type'] === 'datetime') {
                    
class DateValidator extends ConstraintValidator
{
    public const PATTERN = '/^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$/';

    /** * Checks whether a date is valid. * * @internal */
    public static function checkDate(int $year, int $month, int $day): bool
    {
        return checkdate($month$day$year);
    }

    /** * @return void */
    public function validate(mixed $value, Constraint $constraint)
    {
        if (!$constraint instanceof Date) {
            throw new UnexpectedTypeException($constraint, Date::class);
        }

        

        } else {
            if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
                $this->_format = 'yyyy-MM-dd';
                $this->_error(self::FALSEFORMAT);
                $this->_format = null;
                return false;
            }

            list($year$month$day) = sscanf($value, '%d-%d-%d');

            if (!checkdate($month$day$year)) {
                $this->_error(self::INVALID_DATE);
                return false;
            }
        }

        return true;
    }

    /** * Check if the given date fits the given format * * @param string $value Date to check * @return boolean False when date does not fit the format */

function wp_checkdate( $month$day$year$source_date ) {
    /** * Filters whether the given date is valid for the Gregorian calendar. * * @since 3.5.0 * * @param bool $checkdate Whether the given date is valid. * @param string $source_date Date to check. */
    return apply_filters( 'wp_checkdate', checkdate( $month$day$year )$source_date );
}

/** * Loads the auth check for monitoring whether the user is still logged in. * * Can be disabled with remove_action( 'admin_enqueue_scripts', 'wp_auth_check_load' ); * * This is disabled for certain screens where a login screen could cause an * inconvenient interruption. A filter called {@see 'wp_auth_check_load'} can be used * for fine-grained control. * * @since 3.6.0 */
Home | Imprint | This part of the site doesn't use cookies.