How can PHP be used to properly format dates retrieved from a database for display?
When retrieving dates from a database in PHP, they are often in a standardized format like 'YYYY-MM-DD'. To properly format these dates for display, you can use the `date()` function in PHP to convert them to a more readable format. You can specify the desired format using the appropriate format characters (e.g., 'd/m/Y' for 'day/month/year').
// Retrieve date from database
$dateFromDB = '2022-01-15';
// Format date for display
$formattedDate = date('d/m/Y', strtotime($dateFromDB));
echo $formattedDate; // Output: 15/01/2022
Related Questions
- What is the purpose of using GET to pass variables in PHP and what are the potential issues that can arise when implementing it?
- How can PHP be used to redirect users to a previous page upon clicking "OK" in a pop-up window displaying an error message?
- What could be causing the "Database failure: 1064" error in the PHP code provided?