ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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
42 private const string RFC7231 = 'D, d M Y H:i:s \G\M\T';
43
47 public function transform($from): DateTimeImmutable
48 {
49 if ($from instanceof DateTimeImmutable) {
50 return $from;
51 }
52
53 $formats = [
54 DateTimeInterface::ATOM,
55 DateTimeInterface::COOKIE,
56 DateTimeInterface::ISO8601,
57 DateTimeInterface::RFC822,
58 self::RFC7231, // DateTimeInterface::RFC7231 format (deprecated constant in PHP 8.5)
59 DateTimeInterface::RFC3339_EXTENDED
60 ];
61
62 if (is_string($from)) {
63 foreach ($formats as $format) {
64 $res = DateTimeImmutable::createFromFormat($format, $from);
65 if ($res instanceof DateTimeImmutable) {
66 return $res;
67 }
68 }
69 }
70
71 if (is_int($from) || is_float($from)) {
72 return new DateTimeImmutable("@" . round($from));
73 }
74
76 sprintf('Value "%s" could not be transformed.', var_export($from, true)),
77 'no_valid_datetime',
78 $from
79 );
80 }
81}
Transform date format to DateTimeImmutable Please note:
A transformation is a function from one datatype to another.
$res
Definition: ltiservices.php:69