ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
DateTimeTransformation.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
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:69
Transform date format to DateTimeImmutable Please note:
$format
Definition: metadata.php:235
A transformation is a function from one datatype to another.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$formats
Definition: date.php:77
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...