How to convert date string to date and datetime in PHP
We can use the strtotime()
and date()
methods to convert a date string to date and datetime in PHP.
$input = strtotime('10/06/2020 22:00:02');
$newformat = date('d-m-Y h:i:s',$input);
echo $newformat;
Output:
06-10-2020 10:00:02
You can also extract date and time separately like this.
$input = strtotime('10/06/2020 22:00:02');
$date = date('d-m-Y',$input);
echo $date; // 06-10-2020
time
$input = strtotime('10/06/2020 22:00:02');
$time = date('h:i:s',$input);
echo $time; // 10:00:02