Programming Tutorials

Get the time past between two MySQL dates in PHP

By: Ashley Hunt in PHP Tutorials on 2010-09-29  

I wanted to get the time past from two MySQL dates and came up with this code that does the trick. Supply a start date, end date and optional output date/time format the default is in seconds but will expand from SS to MM:SS and then to HH:MM:SS automatically, you may wish to force a date format that will not be dynamic (site layout etc). See examples below, also see function date() for more date format options.

function calculate_time_past($start_time, $end_time, $format = "s") { 
    $time_span = strtotime($end_time) - strtotime($start_time); 
    if ($format == "s") { // is default format so dynamically calculate date format 
        if ($time_span > 60) { $format = "i:s"; } 
        if ($time_span > 3600) { $format = "H:i:s"; } 
    return gmdate($format, $time_span); 

$start_time = "2007-03-28 00:50:14"; // 00:50:14 will work on its own 
$end_time = "2007-03-28 00:52:59"; // 00:52:59 will also work instead 

echo calculate_time_past($start_time, $end_time) . "<br />"; // will output 02:45 
echo calculate_time_past($start_time, $end_time, "H:i:s"); // will output 00:02:45 when format is overridden 

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in PHP )

Latest Articles (in PHP)