calculate total time from start and end datetime in php
date1 = new DateTime('2006-04-12T12:30:00'); $date2 = new DateTime('2006-04-14T11:30:00'); $diff = $date2->diff($date1); $hours = $diff->h; $hours = $hours + ($diff->days*24); echo $hours;
Here is what the above code is Doing:
1. Create two DateTime objects.
2. Use the diff() method to get the difference between the two objects.
3. Get the number of hours from the diff object.
4. Add the number of days to the number of hours.
5. Echo the result.