本文整理汇总了PHP中Nette\Utils\DateTime::from方法的典型用法代码示例。如果您正苦于以下问题:PHP DateTime::from方法的具体用法?PHP DateTime::from怎么用?PHP DateTime::from使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Nette\Utils\DateTime
的用法示例。
在下文中一共展示了DateTime::from方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createTemplate
/**
* @return Template
*/
public function createTemplate(UI\Control $control = NULL)
{
$latte = $this->latteFactory->create();
$template = new Template($latte);
$presenter = $control ? $control->getPresenter(FALSE) : NULL;
if ($control instanceof UI\Presenter) {
$latte->setLoader(new Loader($control));
}
if ($latte->onCompile instanceof \Traversable) {
$latte->onCompile = iterator_to_array($latte->onCompile);
}
array_unshift($latte->onCompile, function ($latte) use($control, $template) {
$latte->getCompiler()->addMacro('cache', new Nette\Bridges\CacheLatte\CacheMacro($latte->getCompiler()));
UIMacros::install($latte->getCompiler());
if (class_exists(Nette\Bridges\FormsLatte\FormMacros::class)) {
Nette\Bridges\FormsLatte\FormMacros::install($latte->getCompiler());
}
if ($control) {
$control->templatePrepareFilters($template);
}
});
$latte->addFilter('url', 'rawurlencode');
// back compatiblity
foreach (['normalize', 'toAscii', 'webalize', 'padLeft', 'padRight', 'reverse'] as $name) {
$latte->addFilter($name, 'Nette\\Utils\\Strings::' . $name);
}
$latte->addFilter('null', function () {
});
$latte->addFilter('modifyDate', function ($time, $delta, $unit = NULL) {
return $time == NULL ? NULL : Nette\Utils\DateTime::from($time)->modify($delta . $unit);
// intentionally ==
});
if (!isset($latte->getFilters()['translate'])) {
$latte->addFilter('translate', function (Latte\Runtime\FilterInfo $fi) {
throw new Nette\InvalidStateException('Translator has not been set. Set translator using $template->setTranslator().');
});
}
// default parameters
$template->user = $this->user;
$template->baseUri = $template->baseUrl = $this->httpRequest ? rtrim($this->httpRequest->getUrl()->getBaseUrl(), '/') : NULL;
$template->basePath = preg_replace('#https?://[^/]+#A', '', $template->baseUrl);
$template->flashes = [];
if ($control) {
$template->control = $control;
$template->presenter = $presenter;
$latte->addProvider('uiControl', $control);
$latte->addProvider('uiPresenter', $presenter);
$latte->addProvider('snippetBridge', new Nette\Bridges\ApplicationLatte\SnippetBridge($control));
}
$latte->addProvider('cacheStorage', $this->cacheStorage);
// back compatibility
$template->_control = $control;
$template->_presenter = $presenter;
$template->netteCacheStorage = $this->cacheStorage;
if ($presenter instanceof UI\Presenter && $presenter->hasFlashSession()) {
$id = $control->getParameterId('flash');
$template->flashes = (array) $presenter->getFlashSession()->{$id};
}
return $template;
}
示例2: formSucceeded
public function formSucceeded(Form $form)
{
try {
$p = $this->getPresenter();
$values = $form->getValues();
$latest = $this->wikiDraftRepository->getLatestByWiki($this->item);
$start = DateTime::from($values->startTime);
if ($latest && $start < $latest->createdAt) {
throw new Exceptions\WikiDraftConflictException($this->translator->translate('locale.error.newer_draft_created_meanwhile'));
}
unset($values->name);
unset($values->startTime);
$this->wikiDraftRepository->create($values, $this->user, $this->item, new Entities\WikiDraftEntity());
$ent = $this->item;
$p->flashMessage($this->translator->translate('locale.item.updated'));
} catch (Exceptions\WikiDraftConflictException $e) {
$this->newerDraftExists = true;
$this->addFormError($form, $e);
} catch (Exceptions\MissingTagException $e) {
$this->addFormError($form, $e);
} catch (PossibleUniqueKeyDuplicationException $e) {
$this->addFormError($form, $e);
} catch (\Exception $e) {
$this->addFormError($form, $e, $this->translator->translate('locale.error.occurred'));
}
if (!empty($ent)) {
$p->redirect('this');
}
}
示例3: getValue
/**
* @return Nette\Utils\DateTime|NULL
*/
public function getValue()
{
if ($this->value instanceof DateTime) {
// clone
return Nette\Utils\DateTime::from($this->value);
} elseif (is_int($this->value)) {
// timestamp
return Nette\Utils\DateTime::from($this->value);
} elseif (empty($this->value)) {
return NULL;
} elseif (is_string($this->value)) {
$parsers = $this->parsers;
$parsers[] = $this->getDefaultParser();
foreach ($parsers as $parser) {
$value = $parser($this->value);
if ($value instanceof DateTime) {
return $value;
}
}
try {
// DateTime constructor throws Exception when invalid input given
return Nette\Utils\DateTime::from($this->value);
} catch (\Exception $e) {
return NULL;
}
}
return NULL;
}
示例4: createTemplate
/**
* @return UI\ITemplate
*/
protected function createTemplate()
{
/** @var Nette\Bridges\ApplicationLatte\Template $template */
$template = parent::createTemplate();
$latte = $template->getLatte();
$latte->addFilter('texy', function ($input) {
$texy = $this->prepareTexy();
$html = new Nette\Utils\Html();
return $html::el()->setHtml($texy->process($input));
});
$latte->addFilter('vlna', function ($string) {
$string = preg_replace('<([^a-zA-Z0-9])([ksvzaiou])\\s([a-zA-Z0-9]{1,})>i', "\$1\$2 \$3", $string);
// === \xc2\xa0
return $string;
});
$latte->addFilter('dateInWords', function ($time) {
$time = Nette\Utils\DateTime::from($time);
$months = [1 => 'leden', 'únor', 'březen', 'duben', 'květen', 'červen', 'červenec', 'srpen', 'září', 'říjen', 'listopad', 'prosinec'];
return $time->format('j. ') . $months[$time->format('n')] . $time->format(' Y');
});
$latte->addFilter('timeAgoInWords', function ($time) {
$time = Nette\Utils\DateTime::from($time);
$delta = round((time() - $time->getTimestamp()) / 60);
if ($delta == 0) {
return 'před okamžikem';
}
if ($delta == 1) {
return 'před minutou';
}
if ($delta < 45) {
return "před {$delta} minutami";
}
if ($delta < 90) {
return 'před hodinou';
}
if ($delta < 1440) {
return 'před ' . round($delta / 60) . ' hodinami';
}
if ($delta < 2880) {
return 'včera';
}
if ($delta < 43200) {
return 'před ' . round($delta / 1440) . ' dny';
}
if ($delta < 86400) {
return 'před měsícem';
}
if ($delta < 525960) {
return 'před ' . round($delta / 43200) . ' měsíci';
}
if ($delta < 1051920) {
return 'před rokem';
}
return 'před ' . round($delta / 525960) . ' lety';
});
return $template;
}
示例5: fromPlainToEntity
/**
* @param array $data
* @return \FlickrDownloadr\Photo\Photo
*/
public function fromPlainToEntity(array $data, $photoSizeName)
{
$code = $this->photoSizeHelper->getCode($photoSizeName);
$url = $data['url_' . $code];
$width = $data['width_' . $code];
$height = $data['height_' . $code];
$date = \Nette\Utils\DateTime::from($data['datetaken']);
return new Photo($data, $url, $width, $height, $date);
}
示例6: getMeta
/**
* @param SplFileInfo $articleDirectory
* @param string $relativePath
* @return array
*/
private function getMeta($articleDirectory, $relativePath)
{
$filePath = sprintf('%s/%s', $articleDirectory->getRealPath(), self::META_FILE_NAME);
$data = Neon::decode(file_get_contents($filePath));
$data['perex'] = $this->parsedown->text($data['perex']);
$data['publishedAt'] = $data['publishedAt'] ? DateTime::from($data['publishedAt']) : null;
$data['imagePath'] = "{$relativePath}/" . self::IMAGE_NAME;
$data['keyIdentifier'] = $this->parseKeyIdentifier($relativePath);
return $data;
}
示例7: processForm
/**
* @param \Nette\Application\UI\Form $form
*/
public function processForm(Form $form)
{
$values = $form->values;
$event = $this->event ? $this->event : new Entity\Event($this->securityUser->getIdentity());
$event->name = $values->name;
$event->date = DateTime::from($values->date);
$event->place = $values->place;
$event->note = $values->note;
$this->eventFacade->save($event);
}
示例8: deleteOldEmpty
/**
* @return void
*
* Results of this function are periodically deleted
* from production database. Update with caution!
*/
public function deleteOldEmpty()
{
$this->connection->query('
DELETE FROM [video_views]
WHERE [percent] = 0
AND [time] = 0
AND [furthest] = 0
AND [events]::text = "[]"
AND [created_at] < ?
', DateTime::from('7 days ago'));
}
示例9: validate
public function validate()
{
if ($this->createdAt < DateTime::from('- 3 days')) {
throw new TokenExpiredException();
}
if ($this->used) {
throw new TokenAlreadyUsedException();
}
if (!Passwords::verify($this->unsafe, $this->hash)) {
throw new TokenNotValidException();
}
}
示例10: onLogin
/**
* @subscribe
* @param User $user
*/
public function onLogin(User $user)
{
if ($user->getBadges($this->getKey())->count() !== 0) {
// already awarded
return;
}
if ($user->createdAt < DateTime::from('-1 week')) {
$this->awardTo($user, function (Badge $badge, User $user) {
return new Bridges\UserOldWeek($badge, $user);
});
}
}
示例11: createTemplate
/**
* @return Template
*/
public function createTemplate(UI\Control $control)
{
$latte = $this->latteFactory->create();
$template = new Template($latte);
$presenter = $control->getPresenter(FALSE);
if ($control instanceof UI\Presenter) {
$latte->setLoader(new Loader($control));
}
if ($latte->onCompile instanceof \Traversable) {
$latte->onCompile = iterator_to_array($latte->onCompile);
}
array_unshift($latte->onCompile, function ($latte) use ($control, $template) {
$latte->getParser()->shortNoEscape = TRUE;
$latte->getCompiler()->addMacro('cache', new Nette\Bridges\CacheLatte\CacheMacro($latte->getCompiler()));
UIMacros::install($latte->getCompiler());
if (class_exists('Nette\Bridges\FormsLatte\FormMacros')) {
Nette\Bridges\FormsLatte\FormMacros::install($latte->getCompiler());
}
$control->templatePrepareFilters($template);
});
$latte->addFilter('url', 'rawurlencode'); // back compatiblity
foreach (array('normalize', 'toAscii', 'webalize', 'padLeft', 'padRight', 'reverse') as $name) {
$latte->addFilter($name, 'Nette\Utils\Strings::' . $name);
}
$latte->addFilter('null', function () {});
$latte->addFilter('length', function ($var) {
return is_string($var) ? Nette\Utils\Strings::length($var) : count($var);
});
$latte->addFilter('modifyDate', function ($time, $delta, $unit = NULL) {
return $time == NULL ? NULL : Nette\Utils\DateTime::from($time)->modify($delta . $unit); // intentionally ==
});
// default parameters
$template->control = $template->_control = $control;
$template->presenter = $template->_presenter = $presenter;
$template->user = $this->user;
$template->netteHttpResponse = $this->httpResponse;
$template->netteCacheStorage = $this->cacheStorage;
$template->baseUri = $template->baseUrl = $this->httpRequest ? rtrim($this->httpRequest->getUrl()->getBaseUrl(), '/') : NULL;
$template->basePath = preg_replace('#https?://[^/]+#A', '', $template->baseUrl);
$template->flashes = array();
if ($presenter instanceof UI\Presenter && $presenter->hasFlashSession()) {
$id = $control->getParameterId('flash');
$template->flashes = (array) $presenter->getFlashSession()->$id;
}
return $template;
}
示例12: setValue
/**
* Sets control's value.
*
* @return Date
*/
public function setValue($value)
{
if ($value instanceof \DateTime) {
$this->value = $value;
} else {
if (is_numeric($value) || is_string($value)) {
$this->value = Nette\Utils\DateTime::from($value);
} else {
$this->value = NULL;
}
}
}
示例13: renderStats
public function renderStats()
{
$totalDownloads = [];
// Calculate total downloads
if ($stats = $this->addon->github->extra->get('composer-stats')) {
foreach ($stats['all']['labels'] as $key => $label) {
$totalDownloads[] = ['x' => DateTime::from($label)->format('c'), 'y' => $stats['all']['values'][$key]];
}
}
$this->template->totalDownloads = json_encode($totalDownloads);
$this->template->setFile(__DIR__ . '/templates/stats.latte');
$this->template->render();
}
示例14: setValue
/**
* @param \DateTime
* @return BaseDateTime
*/
public function setValue($value)
{
try {
if ($value instanceof DateTime || $value instanceof \DibiDateTime) {
parent::setValue($value->format($this->format));
} elseif ($value != '') {
$date = DateTime::from($value);
parent::setValue($date->format($this->format));
}
} catch (\Exception $e) {
return parent::setValue(NULL);
}
}
示例15: convertToPHPValue
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === NULL) {
return NULL;
} elseif ($value instanceof \DateTime) {
return NetteDateTime::from($value);
}
$val = NetteDateTime::createFromFormat($platform->getDateTimeFormatString(), $value);
if (!$val) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString());
}
return NetteDateTime::from($val);
}