strlen('YYYY-MM-DD')) { // Probably LDAP generalized time syntax $value = substr($value, 0, 8); } // Should be either YYYYMMDD or YYYY-MM-DD if (!preg_match('/^(\d{8}|\d{4}-\d{2}-\d{2})$/', $value)) { throw new InvalidArgumentException("Unknown date format: $value"); } try { return new DateTimeImmutable($value); } catch (Exception $e) { throw new InvalidArgumentException( "Unknown date format: $value", 0, $e, ); } } }