You are here

public static function SmartDateTrait::formatSmartDate in Smart Date 3.3.x

Same name and namespace in other branches
  1. 8.2 src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::formatSmartDate()
  2. 8 src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::formatSmartDate()
  3. 3.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::formatSmartDate()
  4. 3.0.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::formatSmartDate()
  5. 3.1.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::formatSmartDate()
  6. 3.2.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::formatSmartDate()
  7. 3.4.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::formatSmartDate()

Creates a formatted date value as a string.

Parameters

object $start_ts: A timestamp.

object $end_ts: A timestamp.

array $settings: The formatter settings.

string|null $timezone: An optional timezone override.

string $return_type: An optional parameter to force the return value to be a string.

Return value

string|array A formatted date range using the chosen format.

11 calls to SmartDateTrait::formatSmartDate()
Instances::buildRow in modules/smart_date_recur/src/Controller/Instances.php
Builds a row for an instance in the listing.
SmartDateDailyRangeFormatter::viewElements in modules/smart_date_recur/src/Plugin/Field/FieldFormatter/SmartDateDailyRangeFormatter.php
SmartDateDefaultFormatter::getAvailableSmartDateFormatOptions in src/Plugin/Field/FieldFormatter/SmartDateDefaultFormatter.php
Get an array of available Smart Date format options.
SmartDateDurationFormatter::viewElements in src/Plugin/Field/FieldFormatter/SmartDateDurationFormatter.php
SmartdateFieldBuilder::build in src/Plugin/diff/Field/SmartdateFieldBuilder.php

... See full list

File

src/SmartDateTrait.php, line 218

Class

SmartDateTrait
Provides friendly methods for smart date range.

Namespace

Drupal\smart_date

Code

public static function formatSmartDate($start_ts, $end_ts, array $settings = [], $timezone = NULL, $return_type = '') {
  $range = [];

  // Don't need to reduce dates unless conditions are met.
  $date_reduce = FALSE;

  // Ensure that empty timezones are NULL to avoid errors.
  if (empty($timezone)) {
    $timezone = NULL;
  }

  // If no formatting parameters provided, use the default settings.
  if (!$settings) {
    $settings = static::loadSmartDateFormat('default');
    if (!$settings) {
      return FALSE;
    }
  }

  // Apply date format from the display config.
  if ($settings['date_format']) {
    $range['start']['date'] = \Drupal::service('date.formatter')
      ->format($start_ts, '', $settings['date_format'], $timezone);
    $range['end']['date'] = \Drupal::service('date.formatter')
      ->format($end_ts, '', $settings['date_format'], $timezone);
    if ($range['start']['date'] == $range['end']['date']) {
      if ($settings['date_first']) {
        unset($range['end']['date']);
      }
      else {
        unset($range['start']['date']);
      }
    }
    else {

      // If a date range and reduce is set, reduce duplication in the dates.
      $date_reduce = $settings['ampm_reduce'];

      // Don't reduce am/pm if spanning more than one day.
      $settings['ampm_reduce'] = FALSE;
    }
  }

  // If not rendering times, we can stop here.
  if (!$settings['time_format']) {
    if ($date_reduce) {

      // Reduce duplication in date only range.
      $range = static::rangeDateReduce($range, $settings, $start_ts, $end_ts, $timezone);
    }
    return static::rangeFormat($range, $settings, $return_type);
  }
  if ($timezone) {
    date_default_timezone_set($timezone);
  }
  $temp_start = date('H:i', $start_ts);
  $temp_end = date('H:i', $end_ts);

  // If one of the dates are missing, they must have been the same.
  if (!isset($range['start']['date']) || !isset($range['end']['date'])) {

    // Check for zero duration.
    if ($temp_start == $temp_end) {
      if ($settings['date_first']) {
        $range['start']['time'] = static::timeFormat($end_ts, $settings, $timezone);
      }
      else {
        $range['end']['time'] = static::timeFormat($end_ts, $settings, $timezone);
      }
      return static::rangeFormat($range, $settings, $return_type);
    }

    // If the conditions that make this necessary aren't met, set to skip.
    if (!$settings['ampm_reduce'] || date('a', $start_ts) != date('a', $end_ts)) {
      $settings['ampm_reduce'] = FALSE;
    }
  }

  // Check for an all-day range.
  if (static::isAllDay($start_ts, $end_ts, $timezone)) {
    if ($settings['allday_label']) {
      if ($settings['date_first'] && isset($range['end']['date']) || empty($range['start']['date'])) {
        $range['end']['time'] = $settings['allday_label'];
      }
      else {
        $range['start']['time'] = $settings['allday_label'];
      }
    }
    if ($date_reduce) {

      // Reduce duplication in date only range.
      $range = static::rangeDateReduce($range, $settings, $start_ts, $end_ts, $timezone);
    }
    return static::rangeFormat($range, $settings, $return_type);
  }
  $range['start']['time'] = static::timeFormat($start_ts, $settings, $timezone, TRUE);
  $range['end']['time'] = static::timeFormat($end_ts, $settings, $timezone);
  return static::rangeFormat($range, $settings, $return_type);
}