Converting a String to Date and DateTime
A PHP string in the format of mm-dd-YYYY (e.g., 10-16-2003) can be converted to a Date and DateTime using the following steps:
Converting to Date:
$time = strtotime('10/16/2003');
$date = date('Y-m-d', $time); echo $date; // 2003-10-16
Converting to DateTime:
$dateTime = DateTime::createFromFormat('m/d/Y', '10/16/2003'); echo $dateTime->format('Y-m-d'); // 2003-10-16
Note:
The above is the detailed content of How Can I Convert a String to a Date and DateTime Object in PHP?. For more information, please follow other related articles on the PHP Chinese website!