You are here

abstract class DateTimeHelper in Content Planner 8

Hierarchy

Expanded class hierarchy of DateTimeHelper

4 files declare their use of DateTimeHelper
Calendar.php in modules/content_calendar/src/Component/Calendar.php
CalendarController.php in modules/content_calendar/src/Controller/CalendarController.php
CalendarEntry.php in modules/content_calendar/src/Component/CalendarEntry.php
content_calendar.module in modules/content_calendar/content_calendar.module
Contains content_calendar.module.

File

modules/content_calendar/src/DateTimeHelper.php, line 7

Namespace

Drupal\content_calendar
View source
abstract class DateTimeHelper {

  /**
   * Regex for mysql date only values.
   */
  const FORMAT_MYSQL_DATE_ONLY_REGEX = '\\d{4}\\-\\d{2}\\-\\d{2}';

  /**
   * Format value for mysql date only values.
   */
  const FORMAT_MYSQL_DATE_ONLY = 'Y-m-d';

  /**
   * Get Month label by its number.
   *
   * @param int $number
   *
   * @return bool|mixed
   */
  public static function getMonthLabelByNumber($number) {
    if (is_numeric($number) && ($number >= 1 && $number <= 12)) {
      $month_labels = [
        1 => t('January'),
        2 => t('February'),
        3 => t('March'),
        4 => t('April'),
        5 => t('May'),
        6 => t('June'),
        7 => t('July'),
        8 => t('August'),
        9 => t('September'),
        10 => t('October'),
        11 => t('November'),
        12 => t('December'),
      ];
      return $month_labels[$number];
    }
    return FALSE;
  }

  /**
   * Get the count of days in a given month of a given year.
   *
   * @param int $month
   * @param int $year
   *
   * @return int
   */
  public static function getDayCountInMonth($month, $year) {
    return cal_days_in_month(CAL_GREGORIAN, $month, $year);
  }

  /**
   * Get the first day of a given month and year.
   *
   * @param int $month
   * @param int $year
   *
   * @return \DateTime
   */
  public static function getFirstDayOfMonth($month, $year) {
    $datetime = new \DateTime();
    $datetime
      ->setDate($year, $month, 1);
    $datetime
      ->setTime(0, 0, 0);
    return $datetime;
  }

  /**
   * @param int $month
   * @param int $year
   *
   * @return \DateTime
   */
  public static function getLastDayOfMonth($month, $year) {
    $datetime = new \DateTime();
    $datetime
      ->setDate($year, $month, 1);
    $datetime
      ->setTime(23, 59, 59);
    $datetime
      ->modify('last day of this month');
    return $datetime;
  }

  /**
   * Convert unix timestamp to Datetime object.
   *
   * @param int $unix_timestamp
   *
   * @return \DateTime
   */
  public static function convertUnixTimestampToDatetime($unix_timestamp) {
    $datetime = new \DateTime();
    $datetime
      ->setTimestamp($unix_timestamp);
    return $datetime;
  }

  /**
   * Check is a given string is a date of the MySQL Date Only format.
   *
   * @param string $value
   *
   * @return false|int
   */
  public static function dateIsMySQLDateOnly($value) {
    return preg_match("/" . self::FORMAT_MYSQL_DATE_ONLY_REGEX . "/", $value);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DateTimeHelper::convertUnixTimestampToDatetime public static function Convert unix timestamp to Datetime object.
DateTimeHelper::dateIsMySQLDateOnly public static function Check is a given string is a date of the MySQL Date Only format.
DateTimeHelper::FORMAT_MYSQL_DATE_ONLY constant Format value for mysql date only values.
DateTimeHelper::FORMAT_MYSQL_DATE_ONLY_REGEX constant Regex for mysql date only values.
DateTimeHelper::getDayCountInMonth public static function Get the count of days in a given month of a given year.
DateTimeHelper::getFirstDayOfMonth public static function Get the first day of a given month and year.
DateTimeHelper::getLastDayOfMonth public static function
DateTimeHelper::getMonthLabelByNumber public static function Get Month label by its number.