To extract the date from a DateTime object in PHP, you can use the format()
method along with the desired format string. In this case, the date format "Y-m-d" can be used to represent the date in the format "Year-Month-Day".
Here's an example:
$dateTime = new DateTime(); // Create a new DateTime object
$date = $dateTime->format("Y-m-d"); // Extract the date in "Y-m-d" format
echo $date; // Output: e.g., "2022-01-01"
In this example, the current date and time are stored in the $dateTime
object. Then, the format()
method is used to extract the date in the desired "Y-m-d" format and store it in the $date
variable. Finally, the extracted date is displayed using the echo
statement.