You are here

function DateSqlHandler::views_formats in Date 8

@todo.

File

date_api/lib/Drupal/date_api/DateSqlHandler.php, line 831

Class

DateSqlHandler
A class to manipulate date SQL.

Namespace

Drupal\date_api

Code

function views_formats($granularity, $type = 'sql') {
  if (empty($granularity)) {
    return DATE_FORMAT_ISO;
  }
  $formats = array(
    'display',
    'sql',
  );

  // Start with the site long date format and add seconds to it.
  $long = str_replace(':i', ':i:s', variable_get('date_format_long', 'l, F j, Y - H:i'));
  switch ($granularity) {
    case 'year':
      $formats['display'] = 'Y';
      $formats['sql'] = 'Y';
      break;
    case 'month':
      $formats['display'] = DateGranularity::limitFormat($long, array(
        'year',
        'month',
      ));
      $formats['sql'] = 'Y-m';
      break;
    case 'day':
      $formats['display'] = DateGranularity::limitFormat($long, array(
        'year',
        'month',
        'day',
      ));
      $formats['sql'] = 'Y-m-d';
      break;
    case 'hour':
      $formats['display'] = DateGranularity::limitFormat($long, array(
        'year',
        'month',
        'day',
        'hour',
      ));
      $formats['sql'] = 'Y-m-d\\TH';
      break;
    case 'minute':
      $formats['display'] = DateGranularity::limitFormat($long, array(
        'year',
        'month',
        'day',
        'hour',
        'minute',
      ));
      $formats['sql'] = 'Y-m-d\\TH:i';
      break;
    case 'second':
      $formats['display'] = DateGranularity::limitFormat($long, array(
        'year',
        'month',
        'day',
        'hour',
        'minute',
        'second',
      ));
      $formats['sql'] = 'Y-m-d\\TH:i:s';
      break;
    case 'week':
      $formats['display'] = 'F j Y (W)';
      $formats['sql'] = 'Y-\\WW';
      break;
  }
  return $formats[$type];
}