本文整理汇总了PHP中DateTimeImmutable::createFromFormat方法的典型用法代码示例。如果您正苦于以下问题:PHP DateTimeImmutable::createFromFormat方法的具体用法?PHP DateTimeImmutable::createFromFormat怎么用?PHP DateTimeImmutable::createFromFormat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTimeImmutable
的用法示例。
在下文中一共展示了DateTimeImmutable::createFromFormat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: isValidDate
/**
* {@inheritdoc}
*/
public function isValidDate($month_day, $month, $year, &$errors = null)
{
// Create a DateTime object directly.
$datetime = \DateTimeImmutable::createFromFormat('n/j/Y', sprintf('%d/%d/%d', $month, $month_day, $year));
$errors = $datetime->getLastErrors();
return empty($errors['warning_count']) && empty($errors['error_count']);
}
示例2: make
public function make(Str $name, Str $value) : HeaderInterface
{
if ((string) $name->toLower() !== 'date') {
throw new InvalidArgumentException();
}
return new Date(new DateValue(\DateTimeImmutable::createFromFormat(\DateTime::RFC1123, (string) $value)));
}
示例3: itShouldDeserializeADateTimeImmutable
/**
* @test
*/
public function itShouldDeserializeADateTimeImmutable()
{
$customSerializer = new DateTimeCustomSerializer();
$serialized = [1469787919, new \DateTimeZone('+00:00')];
$object = $customSerializer->deserialize($serialized, \DateTimeImmutable::class);
$this->assertEquals($object, \DateTimeImmutable::createFromFormat('U', 1469787919, new \DateTimeZone('+00:00')));
}
示例4: createMessageFromArray
/**
* @param string $messageName
* @param array $messageData
* @throws \UnexpectedValueException
* @return DomainMessage
*/
public function createMessageFromArray($messageName, array $messageData)
{
if (!class_exists($messageName)) {
throw new \UnexpectedValueException('Given message name is not a valid class: ' . (string) $messageName);
}
if (!is_subclass_of($messageName, DomainMessage::class)) {
throw new \UnexpectedValueException(sprintf('Message class %s is not a sub class of %s', $messageName, DomainMessage::class));
}
if (!isset($messageData['message_name'])) {
$messageData['message_name'] = $messageName;
}
if (!isset($messageData['uuid'])) {
$messageData['uuid'] = Uuid::uuid4();
}
if (!isset($messageData['version'])) {
$messageData['version'] = 0;
}
if (!isset($messageData['created_at'])) {
$time = microtime(true);
if (false === strpos($time, '.')) {
$time .= '.0000';
}
$messageData['created_at'] = \DateTimeImmutable::createFromFormat('U.u', $time);
}
if (!isset($messageData['metadata'])) {
$messageData['metadata'] = [];
}
return $messageName::fromArray($messageData);
}
示例5: __invoke
public function __invoke($value)
{
if ($value === NULL) {
return NULL;
}
return \DateTimeImmutable::createFromFormat('U.u', sprintf('%.03f', (double) $value / 1000));
}
示例6: _decodeFromDER
protected static function _decodeFromDER(Identifier $identifier, $data, &$offset)
{
$idx = $offset;
$length = Length::expectFromDER($data, $idx);
$str = substr($data, $idx, $length->length());
$idx += $length->length();
if (!preg_match(self::REGEX, $str, $match)) {
throw new DecodeException("Invalid GeneralizedTime format.");
}
list(, $year, $month, $day, $hour, $minute, $second) = $match;
if (isset($match[7])) {
$frac = $match[7];
// DER restricts trailing zeroes in fractional seconds component
if ($frac[strlen($frac) - 1] === '0') {
throw new DecodeException("Fractional seconds must omit trailing zeroes.");
}
$frac = (int) $frac;
} else {
$frac = 0;
}
$time = $year . $month . $day . $hour . $minute . $second . "." . $frac . self::TZ_UTC;
$dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, self::_createTimeZone(self::TZ_UTC));
if (!$dt) {
throw new DecodeException("Failed to decode GeneralizedTime: " . self::_getLastDateTimeImmutableErrorsStr());
}
$offset = $idx;
return new self($dt);
}
示例7: fromDatabase
/**
* {@inheritdoc}
*/
public function fromDatabase($value)
{
if ($this->immutable) {
return \DateTimeImmutable::createFromFormat($this->format, $value);
}
return \DateTime::createFromFormat($this->format, $value);
}
示例8: toItinerary
/**
* @param RouteCandidateDto $aRouteCandidate
* @return Itinerary
*/
public function toItinerary(RouteCandidateDto $aRouteCandidate)
{
$legs = array();
foreach ($aRouteCandidate->getLegs() as $legDto) {
$legs[] = new Leg($legDto->getLoadLocation(), $legDto->getUnloadLocation(), \DateTimeImmutable::createFromFormat(\DateTime::ATOM, $legDto->getLoadTime()), \DateTimeImmutable::createFromFormat(\DateTime::ATOM, $legDto->getUnloadTime()));
}
return new Itinerary($legs);
}
示例9: testCreateFromFormatFailure
public function testCreateFromFormatFailure()
{
$time = 'foo';
$immutable = DateTimeImmutable::createFromFormat(DateTime::RFC3339, $time);
$mutable = DateTime::createFromFormat(DateTime::RFC3339, $time);
$this->assertFalse($immutable);
$this->assertSame(DateTime::getLastErrors(), DateTimeImmutable::getLastErrors());
}
示例10: __construct
/**
* Create a new audi event (timestamp is initialized here).
*
* @param ProcessEngine $engine
* @param boolean $delay Delay creation of timestamp by 1 millisecond?
*/
public function __construct(ProcessEngine $engine, $delay = false)
{
if ($delay) {
usleep(1000);
}
$this->engine = $engine;
$this->timestamp = \DateTimeImmutable::createFromFormat('U.u', sprintf('%0.3f', microtime(true)));
}
示例11: dateTime
/**
* Get date as a datetime object.
*
* @param string $tz Timezone
* @throws \RuntimeException
* @return \DateTimeImmutable
*/
public function dateTime($tz = "UTC")
{
$dt = \DateTimeImmutable::createFromFormat("!U", $this->value(), self::_createTimeZone($tz));
if (false === $dt) {
throw new \RuntimeException("Failed to create DateTime: " . self::_getLastDateTimeImmutableErrorsStr());
}
return $dt;
}
示例12: unserialize
/**
* @param $serialized
* @return Envelope
*/
public function unserialize($serialized)
{
$data = json_decode($serialized, true);
$messageData = $data['message'];
$messageData['created_at'] = \DateTimeImmutable::createFromFormat('Y-m-d\\TH:i:s.u', $messageData['created_at'], new \DateTimeZone('UTC'));
$proophMessage = $this->messageFactory->createMessageFromArray($messageData['message_name'], $messageData);
$envelope = new Envelope(BernardMessage::fromProophMessage($proophMessage));
return $envelope;
}
示例13: findByToken
public function findByToken($accessTokenId)
{
$qb = $this->dbalConnection->createQueryBuilder();
$stmt = $qb->select('*')->from($this->tableConfiguration->getAccessTokenTableName())->where($qb->expr()->like('access_token', ':accessToken'))->setMaxResults(1)->setParameter('accessToken', $accessTokenId)->execute();
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
if (1 !== count($rows)) {
throw new AccessTokenNotFound($accessTokenId);
}
return new AccessToken($rows[0]['access_token'], \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $rows[0]['expires_at'], new \DateTimeZone('UTC')), $rows[0]['client_id'], $rows[0]['resource_owner_id'] && $rows[0]['resource_owner_type'] ? new ResourceOwner($rows[0]['resource_owner_id'], $rows[0]['resource_owner_type']) : null, explode(',', $rows[0]['scopes']));
}
示例14: parseDateTime
private function parseDateTime($data, array $type)
{
$timezone = isset($type['params'][1]) ? new \DateTimeZone($type['params'][1]) : $this->defaultTimezone;
$format = $this->getFormat($type);
$datetime = \DateTimeImmutable::createFromFormat($format, (string) $data, $timezone);
if (false === $datetime) {
throw new \RuntimeException(sprintf('Invalid datetime "%s", expected format %s.', $data, $format));
}
return $datetime;
}
示例15: createFromFormat
/**
* @param string $format
* @param string $time
* @param \DateTimeZone $timezone
*
* @return \Staffim\DateTime\DateTime
*/
public static function createFromFormat($format, $time, $timezone = null)
{
/**
* @see https://bugs.php.net/bug.php?id=69565
*/
if ($timezone) {
return static::createFromNativeDate(parent::createFromFormat($format, $time, $timezone));
} else {
return static::createFromNativeDate(parent::createFromFormat($format, $time));
}
}