Databases like MySQL store dates in ISO YYYY-MM-DD format (e.g. 2026-08-13), whereas web forms in India, Europe, and Latin America present dates to users in DD-MM-YYYY format. Converting between these date representations in PHP requires robust object-oriented handling.
Object-Oriented Implementation (DateTime::createFromFormat)
<?php
function convertIsoToIndianDate(string $isoDate): string {
// Parse strict YYYY-MM-DD format
$dateObj = DateTime::createFromFormat('Y-m-d', $isoDate);
if (!$dateObj) {
throw new InvalidArgumentException("Invalid ISO date string provided: {$isoDate}");
}
// Output formatted DD-MM-YYYY string
return $dateObj->format('d-m-Y');
}
// Example Execution:
$dbDate = "2026-08-13";
$formattedDate = convertIsoToIndianDate($dbDate);
echo "Formatted Display Date: " . $formattedDate; // Output: 13-08-2026
Comments and corrections