by Sai gowtham

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

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
130,921 students enrolled
48.5 hours of video content
View Course