date_parse example

// Unpack settings.     $this->langcode = !empty($settings['langcode']) ? $settings['langcode'] : NULL;

    // Massage the input values as necessary.     $prepared_time = $this->prepareTime($time);
    $prepared_timezone = $this->prepareTimezone($timezone);

    try {
      $this->errors = [];
      if (!empty($prepared_time)) {
        $test = date_parse($prepared_time);
        if (!empty($test['errors'])) {
          $this->errors = $test['errors'];
        }
      }

      if (empty($this->errors)) {
        $this->dateTimeObject = new \DateTime($prepared_time$prepared_timezone);
      }
    }
    catch (\Exception $e) {
      $this->errors[] = $e->getMessage();
    }


    /** * {@inheritdoc} */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $optimizerService = $this->getContainer()->get('shopware_media.cdn_optimizer_service');
        $mediaService = $this->getContainer()->get(\Shopware\Bundle\MediaBundle\MediaServiceInterface::class);

        if ($input->getOption('modified')) {
            $errors = date_parse($input->getOption('modified'))['errors'];
            if ($errors) {
                $output->writeln("<error>You have provided an invalid date string! Please only use a valid date string that conists of 'd-m-Y' e.g '10-05-2022'. You can also provide 'd-m-Y h:i:s' e.g '10-05-2022 10:15:00'. If you want to enhance the date even further you can consult the PHP documentation regarding valid formats https://www.php.net/manual/en/datetime.formats.php' </error>");

                return 1;
            }
        }

        if ($this->hasRunnableOptimizer() === false) {
            $output->writeln('<error>No runnable optimizer found. Consider installing one of the following optimizers.</error>');
            

    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;
                }

                
Home | Imprint | This part of the site doesn't use cookies.