DateTimePlus example

/** * Returns UTC timestamp of user's TZ 'now'. * * The date field stores date_only values without conversion, considering them * already as UTC. This method returns the UTC equivalent of user's 'now' as a * unix timestamp, so they match using Y-m-d format. * * @return int * Unix timestamp. */
  protected function getUTCEquivalentOfUserNowAsTimestamp() {
    $user_now = new DateTimePlus('now', new \DateTimeZone(date_default_timezone_get()));
    $utc_equivalent = new DateTimePlus($user_now->format('Y-m-d H:i:s')new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));

    return $utc_equivalent->getTimestamp();
  }

  /** * Returns an array formatted date_only values relative to timestamp. * * @param int $timestamp * Unix Timestamp used as 'today'. * * @return array * An array of DateTimeItemInterface::DATE_STORAGE_FORMAT date values. In * order tomorrow, today and yesterday. */

  protected function opBetween($field) {
    $timezone = $this->getTimezone();
    $origin_offset = $this->getOffset($this->value['min']$timezone);

    // Although both 'min' and 'max' values are required, default empty 'min'     // value as UNIX timestamp 0.     $min = (!empty($this->value['min'])) ? $this->value['min'] : '@0';

    // Convert to ISO format and format for query. UTC timezone is used since     // dates are stored in UTC.     $a = new DateTimePlus($minnew \DateTimeZone($timezone));
    $a = $this->query->getDateFormat($this->query->getDateField("'" . $this->dateFormatter->format($a->getTimestamp() + $origin_offset, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset)$this->dateFormat, TRUE);
    $b = new DateTimePlus($this->value['max']new \DateTimeZone($timezone));
    $b = $this->query->getDateFormat($this->query->getDateField("'" . $this->dateFormatter->format($b->getTimestamp() + $origin_offset, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset)$this->dateFormat, TRUE);

    // This is safe because we are manually scrubbing the values.     $operator = strtoupper($this->operator);
    $field = $this->query->getDateFormat($this->query->getDateField($field, TRUE, $this->calculateOffset)$this->dateFormat, TRUE);
    $this->query->addWhereExpression($this->options['group'], "$field $operator $a AND $b");
  }

  /** * Override parent method, which deals with dates as integers. */

  public function testDates($input$timezone$expected) {
    $date = new DateTimePlus($input$timezone);
    $value = $date->format('c');

    if (is_array($input)) {
      $input = var_export($input, TRUE);
    }
    $this->assertEquals($expected$valuesprintf("Test new DateTimePlus(%s, %s): should be %s, found %s.", $input$timezone$expected$value));
  }

  /** * Tests creating dates from string and array input. * * @param mixed $input * Input argument for DateTimePlus. * @param string $timezone * Timezone argument for DateTimePlus. * @param string $expected * Expected output from DateTimePlus::format(). * * @dataProvider providerTestDateArrays */
Home | Imprint | This part of the site doesn't use cookies.