[Solved] How to send NTP Time from ESP32 via MQTT?

Hi, i even bought a ESP32 to send something via MQTT.
It works fine, but how i can send the Time and Date from ESP32 via MQTT ?
I can see it on the seriell Monitor, but it is not working to send it.
This is what i have right now:

#define NTP_SERVER "de.pool.ntp.org"
#define TZ_INFO "WEST-1DWEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00" // Western European Time
struct tm local;
configTzTime(TZ_INFO, NTP_SERVER);
getLocalTime(&local, 5000)
tm local;
getLocalTime(&local);
Serial.println(&local, "Datum: %d.%m.%y Zeit: %H:%M:%S");

I solved it:
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

#define DRUCKMEERESSPIEGEL_HPA (1013.25)
#define SchlafZeit 30E7 // Mikrosekunden hier 90s
#define NTP_SERVER "de.pool.ntp.org"

const char* ssid = "WLANNAME";
const char* password = "GEHEIM";
const char* mqttID = "ESP32mitBME280";
const char* mqttServer = "192.168.0.2";
const char* mqttUser = "User";
const char* mqttPassword = "Passwort";

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
String formattedDate;
String dayStamp;
String timeStamp;

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

Adafruit_BME280 bme; // I2C

unsigned long delayTime;

float temperatur = 0;
float luftfeuchtigkeit = 0;
float luftdruck = 0;
float hoehe = 0;

// LED Pin
const int ledPin = 4;

void setup() {
Serial.begin(115200);
if (!bme.begin(0x76)) {
Serial.println("Keinen BME280 gefunden");
while (1);
}
setup_wifi();
client.setServer(mqttServer, 1883);
client.setCallback(callback);

pinMode(ledPin, OUTPUT);
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(3600);
}

void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Versuche WLAN-Verbindung zu ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WLAN verbunden");
Serial.print("IP: ");
Serial.print(WiFi.localIP());
Serial.print("\r\n");
Serial.println("Hole Zeit und Datum per NTP ...");
}

void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;

for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();

// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
// Changes the output state according to the message
if (String(topic) == "esp32/output") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin, LOW);
}
}
}

void verbindemqtt() {
while (!client.connected()) {
Serial.print("Verbinde zum MQTT Server ... ");
if (client.connect(mqttID, mqttUser, mqttPassword)) {
Serial.println("MQTT-Server verbunden");
} else {
Serial.print("Fehler beim verbinden, rc=");
Serial.print(client.state());
Serial.println(" ich versuche es in 1 Minute nochmal");
delay(60000);
}
}
}
void loop() {
if (!client.connected()) {
verbindemqtt();
}
client.loop();
long now = millis();
if (now - lastMsg > 5000) {
lastMsg = now;
temperatur = bme.readTemperature();
// Convert the value to a char array
char tempString[8];
dtostrf(temperatur, 1, 2, tempString);
Serial.print("Temperatur : ");
Serial.print(tempString);
Serial.print(" °C\r\n");
client.publish("esp32/temperatur", tempString);

luftdruck = bme.readPressure()/ 100.0F;   

// Convert the value to a char array
char druckString[8];
dtostrf(luftdruck, 1, 2, druckString);
Serial.print("Luftdruck : ");
Serial.print(druckString);
Serial.print(" hPa\r\n");
client.publish("esp32/luftdruck", druckString);

luftfeuchtigkeit = bme.readHumidity();

// Convert the value to a char array
char feuchtString[8];
dtostrf(luftfeuchtigkeit, 1, 2, feuchtString);
Serial.print("Luftfeuchtigkeit : ");
Serial.print(feuchtString);
Serial.print(" %\r\n");
client.publish("esp32/luftfeuchtigkeit", feuchtString);

hoehe = bme.readAltitude(DRUCKMEERESSPIEGEL_HPA);

char hoeheString[8];
dtostrf(hoehe, 1, 2, hoeheString);
Serial.print("Berechnete Höhe überm Meeresspiegel : ");
Serial.print(hoeheString);
Serial.print(" m\r\n");
client.publish("esp32/hoehe", hoeheString);

while(!timeClient.update()) {
timeClient.forceUpdate();
}
formattedDate = timeClient.getFormattedDate();
// Serial.println(formattedDate);

// Extract date
int splitT = formattedDate.indexOf("T");
dayStamp = formattedDate.substring(0, splitT);
Serial.print("Datum: ");
Serial.println(dayStamp);
client.publish("esp32/datum", dayStamp.c_str());
// Extract time
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
Serial.print("Uhrzeit: ");
Serial.println(timeStamp);
client.publish("esp32/uhrzeit", timeStamp.c_str());
delay(1000);

Serial.println("Loop Ende, ich gehe jetzt schlafen \r\n \r\n");
esp_sleep_enable_timer_wakeup(SchlafZeit);
esp_deep_sleep_start();
}
}

your code is not readable on smartphones.
please read the in the forum how to - how to format code and edit your post accordingly.