本文整理汇总了PHP中DateTimeHelper::fromString方法的典型用法代码示例。如果您正苦于以下问题:PHP DateTimeHelper::fromString方法的具体用法?PHP DateTimeHelper::fromString怎么用?PHP DateTimeHelper::fromString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTimeHelper
的用法示例。
在下文中一共展示了DateTimeHelper::fromString方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: prepDate
public function prepDate($data, $field)
{
return DateTimeHelper::formatTimeForDb(DateTimeHelper::fromString($data, craft()->timezone));
}
示例2: prepForFieldType
//.........这里部分代码省略.........
}
}
// Find matching element in sources
$criteria = craft()->elements->getCriteria(ElementType::User);
$criteria->groupId = $groupIds;
$criteria->limit = $settings->limit;
// Get search strings
$search = ArrayHelper::stringToArray($data);
// Ability to import multiple Users at once
$data = array();
// Loop through keywords
foreach ($search as $query) {
// Search
$criteria->search = $query;
// Add to data
$data = array_merge($data, $criteria->ids());
}
} else {
// Return empty array
$data = array();
}
break;
case ImportModel::FieldTypeTags:
// Get field settings
$settings = $field->getFieldType()->getSettings();
// Get tag group id
$source = $settings->getAttribute('source');
list($type, $groupId) = explode(':', $source);
$tags = ArrayHelper::stringToArray($data);
$data = array();
foreach ($tags as $tag) {
// Find existing tag
$criteria = craft()->elements->getCriteria(ElementType::Tag);
$criteria->title = $tag;
$criteria->groupId = $groupId;
if (!$criteria->total()) {
// Create tag if one doesn't already exist
$newtag = new TagModel();
$newtag->getContent()->title = $tag;
$newtag->groupId = $groupId;
// Save tag
if (craft()->tags->saveTag($newtag)) {
$tagArray = array($newtag->id);
}
} else {
$tagArray = $criteria->ids();
}
// Add tags to data array
$data = array_merge($data, $tagArray);
}
break;
case ImportModel::FieldTypeNumber:
// Parse as number
$data = LocalizationHelper::normalizeNumber($data);
// Parse as float
$data = floatval($data);
break;
case ImportModel::FieldTypeDate:
// Parse date from string
$data = DateTimeHelper::formatTimeForDb(DateTimeHelper::fromString($data, craft()->timezone));
break;
case ImportModel::FieldTypeRadioButtons:
case ImportModel::FieldTypeDropdown:
//get field settings
$settings = $field->getFieldType()->getSettings();
//get field options
$options = $settings->getAttribute('options');
// find matching option label
$labelSelected = false;
foreach ($options as $option) {
if ($labelSelected) {
continue;
}
if ($data == $option['label']) {
$data = $option['value'];
//stop looking after first match
$labelSelected = true;
}
}
break;
case ImportModel::FieldTypeCheckboxes:
case ImportModel::FieldTypeMultiSelect:
// Convert to array
$data = ArrayHelper::stringToArray($data);
break;
case ImportModel::FieldTypeLightSwitch:
// Convert yes/no values to boolean
switch ($data) {
case Craft::t('Yes'):
$data = true;
break;
case Craft::t('No'):
$data = false;
break;
}
break;
}
}
return $data;
}
示例3: prepForFieldType
/**
* Prepare fields for fieldtypes.
*
* @param string &$data
* @param string $handle
*
* @return mixed
*/
public function prepForFieldType(&$data, $handle)
{
// Fresh up $data
$data = StringHelper::convertToUTF8($data);
$data = trim($data);
// Get field info
$field = craft()->fields->getFieldByHandle($handle);
// If it's a field ofcourse
if (!is_null($field)) {
// For some fieldtypes the're special rules
switch ($field->type) {
case ImportModel::FieldTypeEntries:
// No newlines allowed
$data = str_replace("\n", '', $data);
$data = str_replace("\r", '', $data);
// Don't connect empty fields
if (!empty($data)) {
$data = $this->prepEntriesFieldType($data, $field);
} else {
$data = array();
}
break;
case ImportModel::FieldTypeCategories:
if (!empty($data)) {
$data = $this->prepCategoriesFieldType($data, $field);
} else {
$data = array();
}
break;
case ImportModel::FieldTypeAssets:
if (!empty($data)) {
$data = $this->prepAssetsFieldType($data, $field);
} else {
$data = array();
}
break;
case ImportModel::FieldTypeUsers:
if (!empty($data)) {
$data = $this->prepUsersFieldType($data, $field);
} else {
$data = array();
}
break;
case ImportModel::FieldTypeTags:
$data = $this->prepTagsFieldType($data, $field);
break;
case ImportModel::FieldTypeNumber:
// Parse as numberx
$data = LocalizationHelper::normalizeNumber($data);
// Parse as float
$data = floatval($data);
break;
case ImportModel::FieldTypeDate:
// Parse date from string
$data = DateTimeHelper::formatTimeForDb(DateTimeHelper::fromString($data, craft()->timezone));
break;
case ImportModel::FieldTypeRadioButtons:
case ImportModel::FieldTypeDropdown:
//get field settings
$data = $this->prepDropDownFieldType($data, $field);
break;
case ImportModel::FieldTypeCheckboxes:
case ImportModel::FieldTypeMultiSelect:
// Convert to array
$data = ArrayHelper::stringToArray($data);
break;
case ImportModel::FieldTypeLightSwitch:
// Convert yes/no values to boolean
switch ($data) {
case Craft::t('Yes'):
$data = true;
break;
case Craft::t('No'):
$data = false;
break;
}
break;
}
}
return $data;
}