I have a big problem for getting/computing the minutes.
Scenario:
I have a form which is the user can input the seconds [5s, 10s, 25s, 30s, 60s]. I called the table "Duration"
I already have "Duration" [compose of minutes:seconds], in my database which is "1:5" [the last input]. Then I insert again another seconds "10s"...
*the format is minutes:seconds
The correct output should be: 1:15
The current result is : 0:15
AS we see. I have a problem in computing the minutes. The codes I will show is good for subtracting the seconds. But now I need to revised it to adding the seconds.
Here's my code:
$duration = $_POST["duration"];
if(sizeof($bldg) == 1)
{
$total = sizeof($bldg)-1;
}
else
{
$total = sizeof($bldg)%2;
}
for($i=0; $i<sizeof($bldg);$i++)
{
$result = mysql_query("SELECT fldTotalDuration FROM tbldata WHERE fldNetname = '".$network."' AND fldBldgName = '".$bldg[$i]."' AND fldWeek = '".$week."' AND fldMonth = '".$month."' ");
if(mysql_num_rows($result)==0)
{
$totalduration = "";
$seconds = 0;
$minutes = 0;
$computeSecMin = $seconds * $minutes;
$subSecMin = $computeSecMin + $duration;
$getMin = floor($subSecMin/60);
$getSec = $subSecMin%60;
$totalduration = $getMin .":". $getSec;
}
else{
$row = mysql_fetch_array($result);
$time = explode(":",$row['fldTotalDuration']);
$dur = explode(" ",$duration);
$computeSecMin = 60 * $time[0];
$subSecMin = $computeSecMin + $time[1] + $dur[0];
$getMin = floor($subSecMin/60);
$getSec = $subSecMin%60;
$totalduration = $getMin .":". $getSec;
}
}
$query = "INSERT INTO tbldata(fldNetname,fldBldgName,fldPlaylist,fldMonth,fldWeek,fldDuration,fldFrom,fldTo,fldTotalDuration) VALUES ('".$network."','".$bldg[$i]."','".$AdName."','".$month."','".$week."','".$duration."','".$from."','".$to."', '".$totalduration."')";
mysql_query($query) or die (mysql_error());
$duration = is came from another form where its a combobox/dropdown that is consists of 5s, 10s, 25s, 30s, 60s.
Currently, for adding the seconds is okay but for minutes is not good.
my problem for the computation is this "$getMin = floor($subSecMin/60); " .The result of this is "0" but it should be "1" because this computation is for "minutes".
Thanks for helping me with this problem.
Actually, what you got is what you should get. If
$time = [0, 5]
and
$dur = [10, s]
then
$computeSecMin = 60 * $time[0] = 60*0 = 0
and
$subSecMin = $computeSecMin + $time[1] + $dur[0] = 0 + 5 + 10 = 15
Thus
$getMin = floor($subSecMin/60) = floor(15/60) = floor(0.25) = 0
It seems to me that you are computing it right, and the result is indeed 0 minutes. The problem is your duration variable. It seems to me that your program is getting the wrong value. If I understand, it should be [1, 5]
and not [0,5]
. Check the function that is getting this value.
Here is what you are trying to do (sorry, I changed variables names, yours were not obvious):
$time = array(1,5);
$time_plus = '10s';
$time_plus = preg_replace('#\D#', '', $time_plus);
$minutes = $time [0] + (($time_plus + $time[1] >= 60) ? 1 : 0);
$seconds = ($time[1] + $time_plus) % 60;
echo $new_time = $minutes . ":" . $seconds;
You may find using mysql for time manipulations will be cleaner for your application. AddTime() lets you do what you want with only seconds and minutes.
SELECT ADDTIME('00:32.50','1:27.50');
gives the result of:
01:59:01.000000
So, this adds 32.5 seconds to 1 minute 27.50 seconds = 1 minutes 59 seconds and .01 milliseconds.