You are here

function views_xml_backend_date in Views XML Backend 8

Returns a UNIX timestamp rounded to the specified granularity.

Parameters

string $date: The date to convert.

string $granularity: The granularity.

Return value

int The UNIX timestamp.

2 calls to views_xml_backend_date()
Date::__toString in src/Plugin/views/filter/Date.php
Generates an XPath filter string.
ModuleFuncsTest::testVxbDateInt in tests/src/Unit/ModuleFuncsTest.php
Tests for views_xml_backend_date().
2 string references to 'views_xml_backend_date'
views_xml_backend_views_data in ./views_xml_backend.views.inc
Implements hook_views_data().
Xml::getXpath in src/Plugin/views/query/Xml.php
Returns the XPath object for this query.

File

./views_xml_backend.module, line 47
Hook implementations for views_xml_backend.

Code

function views_xml_backend_date($date, $granularity = 'second') {
  $date = trim($date);

  // strtotime() does not handle year values.
  if (strlen($date) === 4 && (string) (int) $date === $date) {
    $date = 'January 1, ' . $date;
  }
  $timestamp = is_numeric($date) ? $date : strtotime($date);

  // Avoid calling getdate() for second granularity.
  if ($granularity === 'second') {
    return (int) $timestamp;
  }
  $parts = getdate((int) $timestamp);
  switch ($granularity) {
    case 'minute':
      $parts['seconds'] = 0;
      break;
    case 'hour':
      $parts['seconds'] = 0;
      $parts['minutes'] = 0;
      break;
    case 'day':
      $parts['seconds'] = 0;
      $parts['minutes'] = 0;
      $parts['hours'] = 0;
      break;
    case 'month':
      $parts['seconds'] = 0;
      $parts['minutes'] = 0;
      $parts['hours'] = 0;
      $parts['mday'] = 1;
      break;
    case 'year':
      $parts['seconds'] = 0;
      $parts['minutes'] = 0;
      $parts['hours'] = 0;
      $parts['mday'] = 1;
      $parts['mon'] = 1;
      break;
  }
  return (int) mktime($parts['hours'], $parts['minutes'], $parts['seconds'], $parts['mon'], $parts['mday'], $parts['year']);
}