What are best practices for handling date formats in PHP when retrieving dates from a database, particularly when dealing with different date formats like DD.MM.YY?
When retrieving dates from a database with different date formats like DD.MM.YY, it is best to standardize the format to a common one like YYYY-MM-DD to avoid confusion and ensure consistency in your application. You can achieve this by using PHP's date and strtotime functions to convert the date strings to the desired format.
// Retrieve date from database in DD.MM.YY format
$dateFromDatabase = '31.12.21';
// Convert date to YYYY-MM-DD format
$standardDateFormat = date('Y-m-d', strtotime(str_replace('.', '-', $dateFromDatabase)));
echo $standardDateFormat; // Output: 2021-12-31