You are here

datetime.module in Drupal 8

Same filename and directory in other branches
  1. 9 core/modules/datetime/datetime.module

Field hooks to implement a simple datetime field.

File

core/modules/datetime/datetime.module
View source
<?php

/**
 * @file
 * Field hooks to implement a simple datetime field.
 */
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Defines the timezone that dates should be stored in.
 *
 * @deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use
 * \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::STORAGE_TIMEZONE
 * instead.
 *
 * @see https://www.drupal.org/node/2912980
 */
const DATETIME_STORAGE_TIMEZONE = 'UTC';

/**
 * Defines the format that date and time should be stored in.
 *
 * @deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use
 * \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATETIME_STORAGE_FORMAT
 * instead.
 *
 * @see https://www.drupal.org/node/2912980
 */
const DATETIME_DATETIME_STORAGE_FORMAT = 'Y-m-d\\TH:i:s';

/**
 * Defines the format that dates should be stored in.
 *
 * @deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use
 * \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATE_STORAGE_FORMAT
 * instead.
 *
 * @see https://www.drupal.org/node/2912980
 */
const DATETIME_DATE_STORAGE_FORMAT = 'Y-m-d';

/**
 * Implements hook_help().
 */
function datetime_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.datetime':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Datetime module provides a Date field that stores dates and times. It also provides the Form API elements <em>datetime</em> and <em>datelist</em> for use in programming modules. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI module help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href=":datetime_do">online documentation for the Datetime module</a>.', [
        ':field' => Url::fromRoute('help.page', [
          'name' => 'field',
        ])
          ->toString(),
        ':field_ui' => \Drupal::moduleHandler()
          ->moduleExists('field_ui') ? Url::fromRoute('help.page', [
          'name' => 'field_ui',
        ])
          ->toString() : '#',
        ':datetime_do' => 'https://www.drupal.org/documentation/modules/datetime',
      ]) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Managing and displaying date fields') . '</dt>';
      $output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the Date field can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', [
        ':field_ui' => \Drupal::moduleHandler()
          ->moduleExists('field_ui') ? Url::fromRoute('help.page', [
          'name' => 'field_ui',
        ])
          ->toString() : '#',
      ]) . '</dd>';
      $output .= '<dt>' . t('Displaying dates') . '</dt>';
      $output .= '<dd>' . t('Dates can be displayed using the <em>Plain</em> or the <em>Default</em> formatter. The <em>Plain</em> formatter displays the date in the <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format. If you choose the <em>Default</em> formatter, you can choose a format from a predefined list that can be managed on the <a href=":date_format_list">Date and time formats</a> page.', [
        ':date_format_list' => Url::fromRoute('entity.date_format.collection')
          ->toString(),
      ]) . '</dd>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Sets a consistent time on a date without time.
 *
 * The default time for a date without time can be anything, so long as it is
 * consistently applied. If we use noon, dates in most timezones will have the
 * same value for in both the local timezone and UTC.
 *
 * @param $date
 *
 * @deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use
 *   \Drupal\Component\Datetime\DateTimePlus::setDefaultDateTime() or
 *   \Drupal\Core\Datetime\DrupalDateTime::setDefaultDateTime() instead.
 *
 * @see https://www.drupal.org/node/2880931
 */
function datetime_date_default_time($date) {
  @trigger_error('datetime_date_default_time() is deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \\Drupal\\Component\\Datetime\\DateTimePlus::setDefaultDateTime() or \\Drupal\\Core\\Datetime\\DrupalDateTime::setDefaultDateTime() instead. See https://www.drupal.org/node/2880931.', E_USER_DEPRECATED);

  // For maximum BC before this method is removed, we do not use the
  // recommendation from the deprecation method. Instead, we call the setTime()
  // method directly. This allows the method to continue to work with
  // \DateTime, DateTimePlus, and DrupalDateTime objects (and classes that
  // may derive from them).
  $date
    ->setTime(12, 0, 0);
}

Functions

Namesort descending Description
datetime_date_default_time Deprecated Sets a consistent time on a date without time.
datetime_help Implements hook_help().

Constants

Namesort descending Description
DATETIME_DATETIME_STORAGE_FORMAT Deprecated Defines the format that date and time should be stored in.
DATETIME_DATE_STORAGE_FORMAT Deprecated Defines the format that dates should be stored in.
DATETIME_STORAGE_TIMEZONE Deprecated Defines the timezone that dates should be stored in.