本文整理汇总了PHP中Drupal\Core\Datetime\DrupalDateTime::createFromArray方法的典型用法代码示例。如果您正苦于以下问题:PHP DrupalDateTime::createFromArray方法的具体用法?PHP DrupalDateTime::createFromArray怎么用?PHP DrupalDateTime::createFromArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Datetime\DrupalDateTime
的用法示例。
在下文中一共展示了DrupalDateTime::createFromArray方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: valueCallback
/**
* {@inheritdoc}
*
* Validates the date type to adjust 12 hour time and prevent invalid dates.
* If the date is valid, the date is set in the form.
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state)
{
$parts = $element['#date_part_order'];
$increment = $element['#date_increment'];
$date = NULL;
if ($input !== FALSE) {
$return = $input;
if (isset($input['ampm'])) {
if ($input['ampm'] == 'pm' && $input['hour'] < 12) {
$input['hour'] += 12;
} elseif ($input['ampm'] == 'am' && $input['hour'] == 12) {
$input['hour'] -= 12;
}
unset($input['ampm']);
}
$timezone = !empty($element['#date_timezone']) ? $element['#date_timezone'] : NULL;
$date = DrupalDateTime::createFromArray($input, $timezone);
if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
static::incrementRound($date, $increment);
}
} else {
$return = array_fill_keys($parts, '');
if (!empty($element['#default_value'])) {
$date = $element['#default_value'];
if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
static::incrementRound($date, $increment);
foreach ($parts as $part) {
switch ($part) {
case 'day':
$format = 'j';
break;
case 'month':
$format = 'n';
break;
case 'year':
$format = 'Y';
break;
case 'hour':
$format = in_array('ampm', $element['#date_part_order']) ? 'g' : 'G';
break;
case 'minute':
$format = 'i';
break;
case 'second':
$format = 's';
break;
case 'ampm':
$format = 'a';
break;
default:
$format = '';
}
$return[$part] = $date->format($format);
}
}
}
}
$return['object'] = $date;
return $return;
}
示例2: getDateTime
/**
* {@inheritdoc}
*/
public function getDateTime()
{
if ($this->value) {
if (is_array($this->value)) {
$datetime = DrupalDateTime::createFromArray($this->value);
} else {
$datetime = new DrupalDateTime($this->value);
}
return $datetime;
}
}