ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilCertificateDateHelper.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 public function __construct()
24 {
25 class_exists('ilDateTime'); // Ensure all global constants are defined
26 }
27
33 public function formatDate($raw_date_input, ?ilObjUser $user = null, ?int $date_format = null): string
34 {
35 return $this->format(
36 $raw_date_input,
37 $user,
38 $date_format ?? $this->autoDetectFormat($raw_date_input, IL_CAL_DATE),
39 false
40 );
41 }
42
48 public function formatDateTime($raw_datetime_input, ?ilObjUser $user = null, ?int $datetime_format = null): string
49 {
50 return $this->format(
51 $raw_datetime_input,
52 $user,
53 $datetime_format ?? $this->autoDetectFormat($raw_datetime_input, IL_CAL_DATETIME),
54 true
55 );
56 }
57
63 private function format($raw, ?ilObjUser $user, int $format, bool $has_time): string
64 {
65 $this->assertFormatMatchesInput($raw, $format);
66
67 if ($format === IL_CAL_UNIX) {
68 $raw = (int) $raw;
69 } else {
70 $raw = (string) $raw;
71 }
72
75 try {
76 $dateObj = $has_time
77 ? new ilDateTime($raw, $format)
78 : new ilDate($raw, $format);
79
81 $dateObj,
82 false,
83 false,
84 false,
85 $user
86 );
87 } finally {
89 }
90 }
91
95 private function autoDetectFormat($value, int $default_when_not_unix): int
96 {
97 return $this->isProbablyUnixTimestamp($value) ? IL_CAL_UNIX : $default_when_not_unix;
98 }
99
104 private function assertFormatMatchesInput($value, int $format): void
105 {
106 $is_unix_like = $this->isProbablyUnixTimestamp($value);
107
108 if ($format === IL_CAL_UNIX && !$is_unix_like) {
109 throw new InvalidArgumentException('Non-numeric input given for IL_CAL_UNIX');
110 }
111
112 if ($format !== IL_CAL_UNIX && $is_unix_like) {
113 throw new InvalidArgumentException('Unix timestamp given but format is not IL_CAL_UNIX');
114 }
115 }
116
120 private function isProbablyUnixTimestamp($maybe_timestamp): bool
121 {
122 if (is_int($maybe_timestamp)) {
123 return true;
124 }
125
126 if (!is_string($maybe_timestamp) || !ctype_digit($maybe_timestamp)) {
127 return false;
128 }
129
130 try {
131 $datetime = DateTimeImmutable::createFromFormat('Ymd', $maybe_timestamp);
132 return $datetime === false;
133 } catch (Throwable) {
134 // Fallthrough
135 }
136
137 return true;
138 }
139}
$datetime
const IL_CAL_DATE
const IL_CAL_UNIX
const IL_CAL_DATETIME
formatDateTime($raw_datetime_input, ?ilObjUser $user=null, ?int $datetime_format=null)
assertFormatMatchesInput($value, int $format)
autoDetectFormat($value, int $default_when_not_unix)
formatDate($raw_date_input, ?ilObjUser $user=null, ?int $date_format=null)
isProbablyUnixTimestamp($maybe_timestamp)
format($raw, ?ilObjUser $user, int $format, bool $has_time)
static setUseRelativeDates(bool $a_status)
set use relative dates
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
@classDescription Date and time handling
Class for single dates.
User class.