You are here

public static function OfficeHoursDateHelper::format in Office Hours 8

Helper function to convert a time to a given format.

For formatting options:

Parameters

string $time: Time, in 24hr format '0800', '800', '08:00' or '8:00'.

string $time_format: The requested time format.

bool $end_time: TRUE if the time is an End time of a time slot.

Return value

string The formatted time.

See also

https://www.php.net/manual/en/function.date.php

https://www.php.net/manual/en/datetime.formats.time.php

5 calls to OfficeHoursDateHelper::format()
OfficeHoursDateHelper::formatTimeSlot in src/OfficeHoursDateHelper.php
Formats a time slot.
OfficeHoursDatetime::get in src/Element/OfficeHoursDatetime.php
Returns the data from a widget.
OfficeHoursFieldDiffBuilder::build in src/Plugin/diff/Field/OfficeHoursFieldDiffBuilder.php
Builds an array of strings.
OfficeHoursItem::storageSettingsForm in src/Plugin/Field/FieldType/OfficeHoursItem.php
Returns a form for the storage-level settings.
office_hours_tokens in ./office_hours.tokens.inc
Implements hook_tokens().

File

src/OfficeHoursDateHelper.php, line 101

Class

OfficeHoursDateHelper
Defines lots of helpful functions for use in massaging dates.

Namespace

Drupal\office_hours

Code

public static function format($time, $time_format, $end_time = FALSE) {

  // Convert '800' or '0800' to '08:00'.
  if (!mb_strlen($time)) {
    return NULL;
  }

  // Empty time field. (negative value)
  if (strstr($time, '-')) {
    return NULL;
  }
  if (!strstr($time, ':')) {
    $time = substr('0000' . $time, -4);
    $hour = substr($time, 0, -2);
    $min = substr($time, -2);
    $time = $hour . ':' . $min;
  }
  $date = new DrupalDateTime($time);
  $formatted_time = $date
    ->format($time_format);

  // Format the 24-hr end time from 0 to 24:00/2400.
  if ($end_time && mb_strlen($time_format) == strspn($time_format, 'GH:i ')) {
    if ($time == '0:00' || $time == '00:00') {
      $formatted_time = '24:00';
    }
  }
  return $formatted_time;
}