Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP date from MySQL format to swedish month. Error DateTimeInterface

I have a row coming from mysql query like this:

$date=$row['date'];

In MySQL this date is stored like this:

2024-12-28 (Y-m-d)

I want to display this date in Swedish language like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

28 Maj 2024

I am trying it like this:

setlocale(LC_ALL, 'sv_SE');
$date=$row['date'];
echo date_format($date,"d M Y");

But I get error DateTimeInterface, any idea how to achieve this?

>Solution :

Are you sure, $date is already an object?
You need to parse it first. Also date_format can not handle internationalization, you have to use IntlDateFormatter:

$row['date'] = '2024-5-28';

$cal = IntlCalendar::fromDateTime($row['date']);
echo IntlDateFormatter::formatObject($cal, "d MMMM Y", 'sv_SE');

will print

28 maj 2024

see the fiddle here: https://3v4l.org/NWRBj

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading