[wd_asp elements=’search’ ratio=’100%’ id=1]

Time difference – Hours PHP

9th October 2017

Php - Dates & Time

php codehaven


$input = '22:00 - 06:00';
preg_match('/(\d+):(\d+)\D*(\d+):(\d+)/', $input, $matches);
list(, $startHour, $startMin, $endHour, $endMin) = $matches;
$totalMinutes = ($endHour * 60 + $endMin - $startHour * 60 + $startMin + 1440) % 1440;
$hours = floor($totalMinutes / 60);
$mins = $totalMinutes % 60;

echo "works for $hours hours, $mins minutes";