Date format Email contact form

Hey,
I’ve built a simple contact form based on the template in the docs (Email contact form | Kirby CMS)

It works fine but how can i change the date format in the email.
Would like to have a format like: 24.12.2021

 $data = [
            'date' => get('date'),
        ];
try {
                $kirby->email([
                    'template' => 'email',
                    'from'     => 'test@test.de',
                    'replyTo'  => $data['email'],
                    'to'       => 'test2@test.de',
                    'subject'  => esc($data['name']) . '- Anfrage über Webseite',
                    'data'     => [
                        'name'   => esc($data['name']),
                        'email'   => esc($data['email']),
                        'telefon'   => esc($data['telefon']),
                        'date'   => esc($data['date']),
                        'uhrzeit'   => esc($data['uhrzeit']),
                        'ort'   => esc($data['ort']),
                        'text'   => esc($data['text']),
                    ]
                ]);

Thank you for your help.

You can use the PHP date() function with a format string, see PHP: date - Manual

:see_no_evil: :hear_no_evil: :speak_no_evil: OMG it’s so simple!
You are the best as always.
Thank you.

1 Like

Now I see it’s not working as intended.

In the e-mail-template:

<?= $date = date("d.m.y"); ?>

But then the e-mail always contains the current date.
Where does the date() function have to be inserted?

Because that piece of code redefines the data variable.

You can either already format the date when passing the variable to the template

'data'     => [
                        'name'   => esc($data['name']),
                        'email'   => esc($data['email']),
                        'telefon'   => esc($data['telefon']),
                        'date'   => date('Y-m-d', esc($data['date'])),
                        'uhrzeit'   => esc($data['uhrzeit']),
                        'ort'   => esc($data['ort']),
                        'text'   => esc($data['text']),
                    ]

Or echo the formated date

<?= date("d.m.y", $date); ?>

When i use this in controller:


'data'     = [
                        'name'   => esc($data['name']),
                        'email'   => esc($data['email']),
                        'telefon'   => esc($data['telefon']),
                        'date'   => date('Y-m-d', esc($data['date'])),
                        'uhrzeit'   => esc($data['uhrzeit']),
                        'ort'   => esc($data['ort']),
                        'text'   => esc($data['text']),
                    ]

The error occurs:

Undefined variable $data

When i use this in email-template:

<?= date("d.m.y", $date); ?>

The error occurs:

date(): Argument #2 ($timestamp) must be of type ?int, string given

Should’ve been

<?= date("d.m.y", strtotime($date)); ?>

Thanks very much. it is working :+1: