ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ChangeTimezone.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26use DateTimeZone;
27use InvalidArgumentException;
28use DateTimeImmutable;
29
35{
38
39 private DateTimeZone $timezone;
40
41 public function __construct(string $timezone)
42 {
43 if (!in_array($timezone, timezone_identifiers_list(), true)) {
44 throw new InvalidArgumentException("$timezone is not a valid timezone identifier", 1);
45 }
46 $this->timezone = new DateTimeZone($timezone);
47 }
48
52 public function transform($from): DateTimeImmutable
53 {
54 if (!$from instanceof DateTimeImmutable) {
55 throw new InvalidArgumentException("$from is not a DateTimeImmutable-object", 1);
56 }
57
58 $ts = $from->format('Y-m-d H:i:s');
59
60 return new DateTimeImmutable($ts, $this->timezone);
61 }
62}
Change the timezone (and only the timezone) of php's \DateTimeImmutable WITHOUT changing the date-val...
A transformation is a function from one datatype to another.