hai friends I want to find the difference between the two time in php ex 12:50:40 - 12:20:20 any specific method for this subtraction or normal subtraction
Improvement on top of shdowhawk at gmail dot com: 1. Calculate the time difference (from start to end) 2. Time can be provided as formatted (e.g. 2005-04-02 12:11:10) or integer (12233455). 3. Provide a short display, e.g. 12h 3m 23s if (!function_exists('timeDiff')){ function timeDiff($starttime, $endtime, $detailed=false, $short = true){ if(! is_int($starttime)) $starttime = strtotime($starttime); if(! is_int($endtime)) $endtime = strtotime($endtime); $diff = ($starttime >= $endtime ? $starttime - $endtime : $endtime - $starttime); # Set the periods of time $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); $lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600); if($short){ $periods = array("s", "m", "h", "d", "m", "y"); $lengths = array(1, 60, 3600, 86400, 2630880, 31570560); } # Go from decades backwards to seconds $i = sizeof($lengths) - 1; # Size of the lengths / periods in case you change them $time = ""; # The string we will hold our times in while($i >= 0) { if($diff > $lengths[$i-1]) { # if the difference is greater than the length we are checking... continue $val = floor($diff / $lengths[$i-1]); # 65 / 60 = 1. That means one minute. 130 / 60 = 2. Two minutes.. etc $time .= $val . ($short ? '' : ' ') . $periods[$i-1] . ((!$short && $val > 1) ? 's ' : ' '); # The value, then the name associated, then add 's' if plural $diff -= ($val * $lengths[$i-1]); # subtract the values we just used from the overall diff so we can find the rest of the information if(!$detailed) { $i = 0; } # if detailed is turn off (default) only show the first set found, else show all information } $i--; } return $time; } } PHP: source