How can PHP be used to parse and manipulate date strings to extract only the year?
When working with date strings in PHP, we can use the `DateTime` class to easily parse and manipulate dates. To extract only the year from a date string, we can create a `DateTime` object using the date string, and then use the `format()` method with the 'Y' format specifier to retrieve the year.
$dateString = "2022-10-15";
$date = new DateTime($dateString);
$year = $date->format('Y');
echo $year; // Output: 2022
Keywords
Related Questions
- What are some potential issues with splitting a contact form across multiple divs in PHP?
- How can beginners ensure proper syntax when writing PHP scripts with embedded HTML?
- What are some best practices for updating specific fields in a database table without having to resend all the other fields in PHP?