Backup #791
| ID | 791 |
| Dateipfad | /var/www/dev.campus.systemische-tools.de/src/Infrastructure/Formatter/DateFormatter.php |
| Version | 1 |
| Typ |
modified |
| Größe | 3.0 KB |
| Hash | f5350cdd0e38cb0949d933ae03600df7f822b7b499c05a424d10cc5312911ffd |
| Datum | 2025-12-23 08:06:27 |
| Geändert von | claude-code-hook |
| Grund | Claude Code Pre-Hook Backup vor Edit-Operation |
| Datei existiert |
Ja
|
Dateiinhalt
<?php
namespace Infrastructure\Formatter;
use DateTimeImmutable;
use DateTimeInterface;
/**
* Formats dates consistently across the application.
*/
final class DateFormatter
{
private const GERMAN_MONTHS = [
1 => 'Januar', 2 => 'Februar', 3 => 'März', 4 => 'April',
5 => 'Mai', 6 => 'Juni', 7 => 'Juli', 8 => 'August',
9 => 'September', 10 => 'Oktober', 11 => 'November', 12 => 'Dezember',
];
/**
* Format as short date (dd.mm.yyyy).
*/
public function short(?string $datetime): string
{
if ($datetime === null || $datetime === '') {
return '';
}
return $this->parse($datetime)->format('d.m.Y');
}
/**
* Format as date with time (dd.mm.yyyy HH:mm).
*/
public function dateTime(?string $datetime): string
{
if ($datetime === null || $datetime === '') {
return '';
}
return $this->parse($datetime)->format('d.m.Y H:i');
}
/**
* Format as long German date (1. Januar 2025).
*/
public function longGerman(?string $datetime): string
{
if ($datetime === null || $datetime === '') {
return '';
}
$date = $this->parse($datetime);
return $date->format('j') . '. ' . self::GERMAN_MONTHS[(int) $date->format('n')] . ' ' . $date->format('Y');
}
/**
* Format as ISO 8601 (for APIs).
*/
public function iso(?string $datetime): string
{
if ($datetime === null || $datetime === '') {
return '';
}
return $this->parse($datetime)->format(DateTimeInterface::ATOM);
}
/**
* Format as relative time (vor 5 Minuten, gestern, etc.).
*/
public function relative(?string $datetime): string
{
if ($datetime === null || $datetime === '') {
return '';
}
$date = $this->parse($datetime);
$now = new DateTimeImmutable();
$diff = $now->diff($date);
if ($diff->invert === 0) {
// Future date
return $this->dateTime($datetime);
}
if ($diff->days === 0) {
if ($diff->h === 0) {
if ($diff->i === 0) {
return 'gerade eben';
}
return $diff->i === 1 ? 'vor 1 Minute' : "vor {$diff->i} Minuten";
}
return $diff->h === 1 ? 'vor 1 Stunde' : "vor {$diff->h} Stunden";
}
if ($diff->days === 1) {
return 'gestern';
}
if ($diff->days < 7) {
return "vor {$diff->days} Tagen";
}
return $this->short($datetime);
}
/**
* Format time only (HH:mm).
*/
public function time(?string $datetime): string
{
if ($datetime === null || $datetime === '') {
return '';
}
return $this->parse($datetime)->format('H:i');
}
/**
* Parse a datetime string.
*/
private function parse(string $datetime): DateTimeImmutable
{
return new DateTimeImmutable($datetime);
}
}
Vollständig herunterladen
Aktionen
← Zurück zur Übersicht