ILIAS  trunk Revision v11.0_alpha-1761-g6dbbfa7b760
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
DateTimeTransformation.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
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 }
$res
Definition: ltiservices.php:66
Transform date format to DateTimeImmutable Please note:
A transformation is a function from one datatype to another.