How can substr() function be used to manipulate date formats in PHP?
To manipulate date formats in PHP using the substr() function, you can extract specific parts of the date string and rearrange them to create the desired format. For example, if you have a date in the format "YYYY-MM-DD" and want to convert it to "DD-MM-YYYY", you can use substr() to extract the year, month, and day, and then concatenate them in the desired order.
$date = "2022-01-15";
$new_date = substr($date, 8, 2) . "-" . substr($date, 5, 2) . "-" . substr($date, 0, 4);
echo $new_date; // Output: 15-01-2022
Keywords
Related Questions
- How can you optimize image processing in PHP to avoid overwriting the original image during scaling?
- What best practices should be followed when creating dynamic links in PHP that include data from a database?
- How can the functionality of navigating through data sets be optimized in PHP for better performance?