Search code examples
phpmysqljpgraph

PHP + MySQL: Time as a string into numerics


I'm creating a graph of drill times for my project.

Currently in the DB it stores the drill duration as a Time field. Times are retrieved in MM:SS form so 5 minutes 30 seconds is expressed as 05:30 The graphing package I chose (jpgraph) requires numeric values passed to it as an array to graph.

Using the code below I get this:

Array ( [0] => 00:03:25 [1] => 00:04:23 [2] => 00:03:48 [3] => 00:02:48 [4] => 00:03:09 [5] => 00:02:25 )

I'm guessing how the 00:03:25 is stored as a string (any way to confirm the dynamic type?).

What I probably need is either:

A) Expressed as Decimal Minutes:

Array ( [0] => 3.417 [1] => 4.383 [2] => 3.800 [3] => 2.800 [4] => 3.150 [5] => 2.433 )

B) Expressed as Integer Seconds:

Array ( [0] => 205 [1] => 267 [2] => 228 [3] => 168 [4] => 189 [5] => 145 )

Are there any existing features which do this already or do I have to regex away to accomplish something like this since PHP idate function seems appropriate but only seems to return an array of 0s to me (See code below)...

When I fetch from SQL, I do it with the following functions I made:

public static function queryDB($aSQLQuery,&$aResult,&$aNumResult){
    $aResult = mysql_query($aSQLQuery);
    $aNumResult = mysql_num_rows($aResult);
}

//Returns User aIDNum's 6 most recents attempts of aExcerNum
public static function getTimes($aIDNum, $aExcerNum, &$aResult,&$aNumResult){
    $query = "  SELECT * FROM (
                    SELECT *
                    FROM Times
                    WHERE   uid =   ".$aIDNum."     AND
                            exid =  ".$aExcerNum."  
                    ORDER BY Date DESC
                    LIMIT 6
                ) AS T1 ORDER BY Date ASC;";
    DBHelper::queryDB($query, $aResult, $aNumResult);
}

public static function processTimeData($aResult, $aNumResult, &$aArray){
    for( $i=0; $i<$aNumResult; $i++){
        $row = mysql_fetch_array($aResult);
        $aArray[$i] = DBHelper::txtP($row, 'Date');
    }
    $paddingData = 6 - $aNumResult;
    for ( $j=0; $j < $paddingData; $j++){
        $aArray[ ($j+$aNumResult) ] = 0;
    }
}

public static function txtP($aRow, $aString){
    return htmlspecialchars(stripslashes($aRow[$aString]));
}

Used like so:

DBHelper::getTimes($userID, $excerciseID, $aResult,$aNumResult);
DBHelper::processTimeData($aResult,$aNumResult,$timeData);

My attempt to use PHP idate():

public static function processTimeData($aResult, $aNumResult, &$aArray){
    for( $i=0; $i<$aNumResult; $i++){
        $row = mysql_fetch_array($aResult);
        //Tried "i" (Minutes) and "s" (seconds)
                    $aArray[$i] = iDate("i",$aRow['Time']);
                    //$aArray[$i] = iDate("s",$aRow['Time']);
    }
    $paddingData = 6 - $aNumResult;
    for ( $j=0; $j < $paddingData; $j++){
        $aArray[ ($j+$aNumResult) ] = 0;
    }
}

results in:

Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 )


Solution

  • Found myself a solution:

    Thanks to Ragnar for helping me think of something!

    processTimeData changed as below:

    public static function processTimeData($aResult, $aNumResult, &$aArray){
        for( $i=0; $i<$aNumResult; $i++){
            $row = mysql_fetch_array($aResult);
            $date = new DateTime( DBHelper::txtP($row,'Time') );
            $aArray[$i] = intval($date->format('s')) + intval($date->format('i'))*60 + intval($date->format('G'))*3600;
        }
        $paddingData = 6 - $aNumResult;
        for ( $j=0; $j < $paddingData; $j++){
            $aArray[ ($j+$aNumResult) ] = 0;
        }
    }