You are here

public static function DateGranularity::format in Date 8

Constructs a valid DATETIME format string, limited to a certain granularity.

File

date_api/lib/Drupal/date_api/DateGranularity.php, line 249
Definition of DateGranularity.

Class

DateGranularity
This class manages granularity. It can set granularity, get it from an array, get it from a format string, see if the array has any time or date elements, set and unset various granularity parts, create a nongranularity array of the granularity parts…

Namespace

Drupal\date_api

Code

public static function format($granularity) {
  if (is_array($granularity)) {
    $granularity = self::precision($granularity);
  }
  $format = 'Y-m-d H:i:s';
  switch ($granularity) {
    case 'year':
      return substr($format, 0, 1);
    case 'month':
      return substr($format, 0, 3);
    case 'day':
      return substr($format, 0, 5);
    case 'hour':
      return substr($format, 0, 7);
    case 'minute':
      return substr($format, 0, 9);
    default:
      return $format;
  }
}