format(self::DB_DATETIME); } /** * Formats a DateTimeImmutable object to a database-friendly DATE string. */ public static function getDbDate(DateTimeImmutable $date): string { return $date->format(self::DB_DATE); } /** * Gets the hour from a DateTimeImmutable object (00-23). */ public static function getHour(DateTimeImmutable $date): string { return $date->format('H'); } /** * Gets the week number from a DateTimeImmutable object (01-53). */ public static function getWeekNumber(DateTimeImmutable $date): string { return $date->format('W'); } /** * Gets the day number of the year from a DateTimeImmutable object (0-365). */ public static function getDayOfYear(DateTimeImmutable $date): string { return $date->format('z'); } /** * Gets the day of the week from a DateTimeImmutable object (1 for Monday, 7 for Sunday). */ public static function getDayOfWeek(DateTimeImmutable $date): string { return $date->format('N'); } /** * Checks if the given date falls on a weekend (Saturday or Sunday). */ public static function isWeekend(DateTimeImmutable $date): bool { return (int) $date->format('N') >= 6; } /** * Gets a formatted timestamp for file names, including milliseconds. */ public static function getFileTimestamp(DateTimeImmutable $date): string { return $date->format(self::FILE_TIMESTAMP); } /** * Inserts a timestamp into a filename before its extension. * * @throws Exception */ public static function insertTimestampToFileName(string $fileName): string { $info = pathinfo($fileName); $timestamp = self::getFileTimestamp(self::now()); $baseName = $info['filename'] ?? ''; $extension = $info['extension'] ?? ''; $newFileName = $baseName . '-' . $timestamp; if (!empty($extension)) { $newFileName .= '.' . $extension; } return $newFileName; } }