php date convert code example
Example 1: php change date format
To convert the date-time format PHP provides strtotime() and date() function. We change the date format from one format to another.
Change YYYY-MM-DD to DD-MM-YYYY
<? php.
$currDate = "2020-04-18";
$changeDate = date("d-m-Y", strtotime($currDate));
echo "Changed date format is: ". $changeDate. " (MM-DD-YYYY)";
?>
Example 2: php convert string to date
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
echo $newformat;
Example 3: php string to date
$s = '06/10/2011 19:00:02';
$date = strtotime($s);
echo date('d/M/Y H:i:s', $date);
The above one is the one of the example of converting a string to date.
echo $s ->format('Y-m-d');
The above one is another method
Example 4: change date format php
$currDate = "2020-08-25";
$changeDate = date("d-m-Y", strtotime($currDate));
echo $changedDate;
Example 5: php string to date
$s = '06/10/2011 19:00:02';$date = strtotime($s);echo date('d/M/Y H:i:s', $date); The above one is the one of the example of converting a string to date. echo $s ->format('Y-m-d'); The above one is another method