Skip to content
DateHelper.php 15.6 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
<?php
/**
 * @file
 * Contains \Drupal\datetime\DateHelper.
 *
 * Lots of helpful functions for use in massaging dates, specific to the the
 * Gregorian calendar system. The values include both translated and
 * untranslated values.
 *
 * Untranslated values are useful as array keys and as css identifiers, and
 * should be listed in English.
 *
 * Translated values are useful for display to the user. All values that need
 * translation should be hard-coded and wrapped in t() so the translation system
 * will be able to process them.
 */
namespace Drupal\datetime;

use Drupal\Core\Datetime\DrupalDateTime;

/**
 * Defines Gregorian Calendar date values.
 */
class DateHelper {

  /**
   * Constructs an untranslated array of month names.
   *
   * @return array
   *   An array of month names.
   */
  public static function monthNamesUntranslated() {
    // Force the key to use the correct month value, rather than
    // starting with zero.
    return array(
      1  => 'January',
      2  => 'February',
      3  => 'March',
      4  => 'April',
      5  => 'May',
      6  => 'June',
      7  => 'July',
      8  => 'August',
      9  => 'September',
      10 => 'October',
      11 => 'November',
      12 => 'December',
    );
  }

  /**
   * Constructs an untranslated array of abbreviated month names.
   *
   * @return array
   *   An array of month names.
   */
  public static function monthNamesAbbrUntranslated() {
    // Force the key to use the correct month value, rather than
    // starting with zero.
    return array(
      1  => 'Jan',
      2  => 'Feb',
      3  => 'Mar',
      4  => 'Apr',
      5  => 'May',
      6  => 'Jun',
      7  => 'Jul',
      8  => 'Aug',
      9  => 'Sep',
      10 => 'Oct',
      11 => 'Nov',
      12 => 'Dec',
    );
  }

  /**
   * Returns a translated array of month names.
   *
   * @param bool $required
   *   (optional) If FALSE, the returned array will include a blank value.
   *   Defaults to FALSE.
   *
   * @return array
   *   An array of month names.
   */
  public static function monthNames($required = FALSE) {
    // Force the key to use the correct month value, rather than
    // starting with zero.
    $monthnames = array(
      1  => t('January', array(), array('context' => 'Long month name')),
      2  => t('February', array(), array('context' => 'Long month name')),
      3  => t('March', array(), array('context' => 'Long month name')),
      4  => t('April', array(), array('context' => 'Long month name')),
      5  => t('May', array(), array('context' => 'Long month name')),
      6  => t('June', array(), array('context' => 'Long month name')),
      7  => t('July', array(), array('context' => 'Long month name')),
      8  => t('August', array(), array('context' => 'Long month name')),
      9  => t('September', array(), array('context' => 'Long month name')),
      10 => t('October', array(), array('context' => 'Long month name')),
      11 => t('November', array(), array('context' => 'Long month name')),
      12 => t('December', array(), array('context' => 'Long month name')),
    );
    $none = array('' => '');
    return !$required ? $none + $monthnames : $monthnames;
  }

  /**
   * Constructs a translated array of month name abbreviations
   *
   * @param bool $required
   *   (optional) If FALSE, the returned array will include a blank value.
   *   Defaults to FALSE.
   *
   * @return array
   *   An array of month abbreviations.
   */
  public static function monthNamesAbbr($required = FALSE) {
    // Force the key to use the correct month value, rather than
    // starting with zero.
    $monthnames = array(
      1  => t('Jan'),
      2  => t('Feb'),
      3  => t('Mar'),
      4  => t('Apr'),
      5  => t('May'),
      6  => t('Jun'),
      7  => t('Jul'),
      8  => t('Aug'),
      9  => t('Sep'),
      10 => t('Oct'),
      11 => t('Nov'),
      12 => t('Dec'),
    );
    $none = array('' => '');
    return !$required ? $none + $monthnames : $monthnames;
  }

  /**
   * Constructs an untranslated array of week days.
   *
   * @return array
   *   An array of week day names
   */
  public static function weekDaysUntranslated() {
    return array(
      'Sunday',
      'Monday',
      'Tuesday',
      'Wednesday',
      'Thursday',
      'Friday',
      'Saturday',
    );
  }

  /**
   * Returns a translated array of week names.
   *
   * @param bool $required
   *   (optional) If FALSE, the returned array will include a blank value.
   *   Defaults to FALSE.
   *
   * @return array
   *   An array of week day names
   */
  public static function weekDays($required = FALSE) {
    $weekdays = array(
      t('Sunday'),
      t('Monday'),
      t('Tuesday'),
      t('Wednesday'),
      t('Thursday'),
      t('Friday'),
      t('Saturday'),
    );
    $none = array('' => '');
    return !$required ? $none + $weekdays : $weekdays;
  }

  /**
   * Constructs a translated array of week day abbreviations.
   *
   * @param bool $required
   *   (optional) If FALSE, the returned array will include a blank value.
   *   Defaults to FALSE.
   *
   * @return array
   *   An array of week day abbreviations
   */
  public static function weekDaysAbbr($required = FALSE) {
    $weekdays = array(
      t('Sun', array(), array('context' => 'Sunday abbreviation')),
      t('Mon', array(), array('context' => 'Monday abbreviation')),
      t('Tue', array(), array('context' => 'Tuesday abbreviation')),
      t('Wed', array(), array('context' => 'Wednesday abbreviation')),
      t('Thu', array(), array('context' => 'Thursday abbreviation')),
      t('Fri', array(), array('context' => 'Friday abbreviation')),
      t('Sat', array(), array('context' => 'Saturday abbreviation')),
    );
    $none = array('' => '');
    return !$required ? $none + $weekdays : $weekdays;
  }

  /**
   * Constructs a translated array of 2-letter week day abbreviations.
   *
   * @param bool $required
   *   (optional) If FALSE, the returned array will include a blank value.
   *   Defaults to FALSE.
   *
   * @return array
   *   An array of week day 2 letter abbreviations
   */
  public static function weekDaysAbbr2($required = FALSE) {
    $weekdays = array(
      t('Su', array(), array('context' => 'Sunday 2 letter abbreviation')),
      t('Mo', array(), array('context' => 'Monday 2 letter abbreviation')),
      t('Tu', array(), array('context' => 'Tuesday 2 letter abbreviation')),
      t('We', array(), array('context' => 'Wednesday 2 letter abbreviation')),
      t('Th', array(), array('context' => 'Thursday 2 letter abbreviation')),
      t('Fr', array(), array('context' => 'Friday 2 letter abbreviation')),
      t('Sa', array(), array('context' => 'Saturday 2 letter abbreviation')),
    );
    $none = array('' => '');
    return !$required ? $none + $weekdays : $weekdays;
  }

  /**
   * Constructs a translated array of 1-letter week day abbreviations.
   *
   * @param bool $required
   *   (optional) If FALSE, the returned array will include a blank value.
   *   Defaults to FALSE.
   *
   * @return array
   *   An array of week day 1 letter abbreviations
   */
  public static function weekDaysAbbr1($required = FALSE) {
    $weekdays = array(
      t('S', array(), array('context' => 'Sunday 1 letter abbreviation')),
      t('M', array(), array('context' => 'Monday 1 letter abbreviation')),
      t('T', array(), array('context' => 'Tuesday 1 letter abbreviation')),
      t('W', array(), array('context' => 'Wednesday 1 letter abbreviation')),
      t('T', array(), array('context' => 'Thursday 1 letter abbreviation')),
      t('F', array(), array('context' => 'Friday 1 letter abbreviation')),
      t('S', array(), array('context' => 'Saturday 1 letter abbreviation')),
    );
    $none = array('' => '');
    return !$required ? $none + $weekdays : $weekdays;
  }

  /**
   * Reorders weekdays to match the first day of the week.
   *
   * @param array $weekdays
   *   An array of weekdays.
   *
   * @return array
   *   An array of weekdays reordered to match the first day of the week.
   */
  public static function weekDaysOrdered($weekdays) {
    $first_day = config('system.date')->get('first_day');
    if ($first_day > 0) {
      for ($i = 1; $i <= $first_day; $i++) {
        $last = array_shift($weekdays);
        array_push($weekdays, $last);
      }
    }
    return $weekdays;
  }

  /**
   * Constructs an array of years in a specified range.
   *
   * @param int $min
   *   (optional) The minimum year in the array. Defaults to zero.
   * @param int $max
   *   (optional) The maximum year in the array. Defaults to zero.
   * @param bool $required
   *   (optional) If FALSE, the returned array will include a blank value.
   *   Defaults to FALSE.
   *
   * @return array
   *   An array of years in the selected range.
   */
  public static function years($min = 0, $max = 0, $required = FALSE) {
    // Ensure $min and $max are valid values.
    if (empty($min)) {
      $min = intval(date('Y', REQUEST_TIME) - 3);
    }
    if (empty($max)) {
      $max = intval(date('Y', REQUEST_TIME) + 3);
    }
    $none = array('' => '');
    $range = drupal_map_assoc(range($min, $max));
    return !$required ? $none + $range : $range;
  }

  /**
   * Constructs an array of days in a month.
   *
   * @param bool $required
   *   (optional) If FALSE, the returned array will include a blank value.
   *   Defaults to FALSE.
   * @param int $month
   *   (optional) The month in which to find the number of days. Defaults to
   *   NULL.
   * @param int $year
   *   (optional) The year in which to find the number of days. Defaults to
   *   NULL.
   *
   * @return array
   *   An array of days for the selected month.
   */
  public static function days($required = FALSE, $month = NULL, $year = NULL) {
    // If we have a month and year, find the right last day of the month.
    if (!empty($month) && !empty($year)) {
      $date = new DrupalDateTime($year . '-' . $month . '-01 00:00:00', 'UTC');
      $max = $date->format('t');
    }
    // If there is no month and year given, default to 31.
    if (empty($max)) {
      $max = 31;
    }
    $none = array('' => '');
    $range = drupal_map_assoc(range(1, $max));
    return !$required ? $none + $range : $range;
  }


  /**
   * Constructs an array of hours.
   *
   * @param string $format
   *   (optional) A date format string that indicates the format to use for the
   *   hours. Defaults to 'H'.
   * @param bool $required
   *   (optional) If FALSE, the returned array will include a blank value.
   *   Defaults to FALSE.
   *
   * @return array
   *   An array of hours in the selected format.
   */
  public static function hours($format = 'H', $required = FALSE) {
    $hours = array();
    if ($format == 'h' || $format == 'g') {
      $min = 1;
      $max = 12;
    }
    else {
      $min = 0;
      $max = 23;
    }
    for ($i = $min; $i <= $max; $i++) {
      $formatted = ($format == 'H' || $format == 'h') ? DrupalDateTime::datePad($i) : $i;
      $hours[$i] = $formatted;
    }
    $none = array('' => '');
    return !$required ? $none + $hours : $hours;
  }

  /**
   * Constructs an array of minutes.
   *
   * @param string $format
   *   (optional) A date format string that indicates the format to use for the
   *    minutes. Defaults to 'i'.
   * @param bool $required
   *   (optional) If FALSE, the returned array will include a blank value.
   *   Defaults to FALSE.
   * @param int $increment
   *   An integer value to increment the values. Defaults to 1.
   *
   * @return array
   *   An array of minutes in the selected format.
   */
  public static function minutes($format = 'i', $required = FALSE, $increment = 1) {
    $minutes = array();
    // Ensure $increment has a value so we don't loop endlessly.
    if (empty($increment)) {
      $increment = 1;
    }
    for ($i = 0; $i < 60; $i += $increment) {
      $formatted = $format == 'i' ? DrupalDateTime::datePad($i) : $i;
      $minutes[$i] = $formatted;
    }
    $none = array('' => '');
    return !$required ? $none + $minutes : $minutes;
  }

  /**
   * Constructs an array of seconds.
   *
   * @param string $format
   *   (optional) A date format string that indicates the format to use for the
   *   seconds. Defaults to 's'.
   * @param bool $required
   *   (optional) If FALSE, the returned array will include a blank value.
   *   Defaults to FALSE.
   * @param int $increment
   *   An integer value to increment the values. Defaults to 1.
   *
   * @return array
   *   An array of seconds in the selected format.
   */
  public static function seconds($format = 's', $required = FALSE, $increment = 1) {
    $seconds = array();
    // Ensure $increment has a value so we don't loop endlessly.
    if (empty($increment)) {
      $increment = 1;
    }
    for ($i = 0; $i < 60; $i += $increment) {
      $formatted = $format == 's' ? DrupalDateTime::datePad($i) : $i;
      $seconds[$i] = $formatted;
    }
    $none = array('' => '');
    return !$required ? $none + $seconds : $seconds;
  }

  /**
   * Constructs an array of AM and PM options.
   *
   * @param bool $required
   *   (optional) If FALSE, the returned array will include a blank value.
   *   Defaults to FALSE.
   *
   * @return array
   *   An array of AM and PM options.
   */
  public static function ampm($required = FALSE) {
    $none = array('' => '');
    $ampm = array(
             'am' => t('am', array(), array('context' => 'ampm')),
             'pm' => t('pm', array(), array('context' => 'ampm')),
            );
    return !$required ? $none + $ampm : $ampm;
  }

  /**
   * Identifies the number of days in a month for a date.
   *
   * @param mixed $date
   *   (optional) A date object, timestamp, or a date string.
   *   Defaults to NULL, which means to use the current date.
   *
   * @return int
   *   The number of days in the month.
   */
  public static function daysInMonth($date = NULL) {
    if (!$date instanceOf DrupalDateTime) {
      $date = new DrupalDateTime($date);
    }
    if (!$date->hasErrors()) {
      return $date->format('t');
    }
    return NULL;
  }

  /**
   * Identifies the number of days in a year for a date.
   *
   * @param mixed $date
   *   (optional) A date object, timestamp, or a date string.
   *   Defaults to NULL, whcih means to use the current date.
   *
   * @return int
   *   The number of days in the year.
   */
  public static function daysInYear($date = NULL) {
    if (!$date instanceOf DrupalDateTime) {
      $date = new DrupalDateTime($date);
    }
    if (!$date->hasErrors()) {
      if ($date->format('L')) {
        return 366;
      }
      else {
        return 365;
      }
    }
    return NULL;
  }

  /**
   * Returns day of week for a given date (0 = Sunday).
   *
   * @param mixed $date
   *   (optional) A date object, timestamp, or a date string.
   *   Defaults to NULL, which means use the current date.
   *
   * @return int
   *   The number of the day in the week.
   */
  public static function dayOfWeek($date = NULL) {
    if (!$date instanceOf DrupalDateTime) {
      $date = new DrupalDateTime($date);
    }
    if (!$date->hasErrors()) {
      return $date->format('w');
    }
    return NULL;
  }

  /**
   * Returns translated name of the day of week for a given date.
   *
   * @param mixed $date
   *   (optional) A date object, timestamp, or a date string.
   *   Defaults to NULL, whcih means use the current date.
   * @param string $abbr
   *   (optional) Whether to return the abbreviated name for that day.
   *   Defaults to TRUE.
   *
   * @return string
   *   The name of the day in the week for that date.
   */
  public static function dayOfWeekName($date = NULL, $abbr = TRUE) {
    if (!$date instanceOf DrupalDateTime) {
      $date = new DrupalDateTime($date);
    }
    $dow = self::dayOfWeek($date);
    $days = $abbr ? self::weekDaysAbbr() : self::weekDays();
    return $days[$dow];
  }

}