ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DateTimeTransformation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27use DateTimeImmutable;
28use DateTimeInterface;
29
38{
41
45 public function transform($from): DateTimeImmutable
46 {
47 if ($from instanceof DateTimeImmutable) {
48 return $from;
49 }
50
51 $formats = [
52 DateTimeInterface::ATOM,
53 DateTimeInterface::COOKIE,
54 DateTimeInterface::ISO8601,
55 DateTimeInterface::RFC822,
56 DateTimeInterface::RFC7231,
57 DateTimeInterface::RFC3339_EXTENDED
58 ];
59
60 if (is_string($from)) {
61 foreach ($formats as $format) {
62 $res = DateTimeImmutable::createFromFormat($format, $from);
63 if ($res instanceof DateTimeImmutable) {
64 return $res;
65 }
66 }
67 }
68
69 if (is_int($from) || is_float($from)) {
70 return new DateTimeImmutable("@" . round($from));
71 }
72
74 sprintf('Value "%s" could not be transformed.', var_export($from, true)),
75 'no_valid_datetime',
76 $from
77 );
78 }
79}
Transform date format to DateTimeImmutable Please note:
A transformation is a function from one datatype to another.
$res
Definition: ltiservices.php:69