Avatar ·

How to convert date format in PHP: from YYYY-MM-DD to DD.MM.YYYY and other variants

Hello everyone! I'm a beginner PHP developer and I've run into a problem with date conversion. I have a form where the user enters a date in the format `YYYY-MM-DD` (e.g., `2025-04-12`), and I need to output it in the format `DD.MM.YYYY` (e.g., `12.04.2025`). I tried simply using `str_replace` to replace hyphens with dots, but that doesn't work because the order of the numbers remains incorrect — the day and month don't swap places.\n\nHere's my code that doesn't produce the desired result:\n\n```php\n$date = "2025-04-12";\n$new_date = str_replace("-", ".", $date);\necho $new_date; // outputs 2025.04.12, but I need 12.04.2025\n```\n\nI've read about the `date()` and `strtotime()` functions, but I don't fully understand how to use them correctly. I tried this:\n\n```php\n$date = "2025-04-12";\n$timestamp = strtotime($date