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
- What are some alternative methods, besides array_filter, that can be used to extract subarrays based on specific criteria in PHP?
- How important is user experience when it comes to horizontal scrolling in a table generated by PHP?
- What are the potential pitfalls when using preg_replace in PHP for string manipulation?