You are here

public static function Carbon::createSafe in Persian Date for Drupal 8 8.4

Create a new safe Carbon instance from a specific date and time.

If any of $year, $month or $day are set to null their now() values will be used.

If $hour is null it will be set to its now() value and the default values for $minute and $second will be their now() values.

If $hour is not null then the default values for $minute and $second will be 0.

If one of the set values is not valid, an \InvalidArgumentException will be thrown.

Parameters

int|null $year:

int|null $month:

int|null $day:

int|null $hour:

int|null $minute:

int|null $second:

\DateTimeZone|string|null $tz:

Return value

static

Throws

InvalidDateException

File

src/Library/Carbon/Carbon.php, line 501

Class

Carbon
A simple API extension for DateTime

Namespace

Drupal\persian_date\Library\Carbon

Code

public static function createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) {
  $fields = array(
    'year' => array(
      0,
      9999,
    ),
    'month' => array(
      0,
      12,
    ),
    'day' => array(
      0,
      31,
    ),
    'hour' => array(
      0,
      24,
    ),
    'minute' => array(
      0,
      59,
    ),
    'second' => array(
      0,
      59,
    ),
  );
  foreach ($fields as $field => $range) {
    if (${$field} !== null && (!is_int(${$field}) || ${$field} < $range[0] || ${$field} > $range[1])) {
      throw new InvalidDateException($field, ${$field});
    }
  }
  $instance = static::create($year, $month, 1, $hour, $minute, $second, $tz);
  if ($day !== null && $day > $instance->daysInMonth) {
    throw new InvalidDateException('day', $day);
  }
  return $instance
    ->day($day);
}