my table has a column which formated in mysql time() format.
when it’s one value assign into php variable called $preRemainOt
i want to re arrange it into nearest 15 minute
function roundTime($whatTime)
{
echo $whatTime."=>";
$roundTime= round ( strtotime($whatTime) / 60 * 15 );
echo date("H:i:s",$roundTime)."<br>";
return date("H:i:s",$roundTime);
}
$validOt =roundTime($preRemainOt);
<pre>
few bad result given from my try as follow:
$preRemainOt=>$validO
03:43:00=>06:55:45
01:05:00=>06:16:15
02:00:00=>06:30:00
but result shuld be as follow:
$preRemainOt=>$validO
03:43:00=>03:45:00
01:05:00=>01:00:00
02:00:00=>02:00:00
1.pls give me an idea.
2.what happen if my remaining ot value($preRemainOt) exceed 24 hour
ex:$preRemainOt='26:43:00' is that doesn’t matter to either php or mysql function.
what is the best mysql data type to store such a ot calculation
update1
after few advices i spilt my function in to three section
function secondToTime($timeStamp)
{
$hours=intval($timeStamp / 3600);
$mins=intval($timeStamp / 60) % 60;
$secs=$timeStamp % 60;
return sprintf("%d:%02d:%02d",$hours, $mins, $secs);
}
function timeToSecond($time='00:00:00')
{
list($hours, $mins, $secs) = explode(':', $time);
$timeToSecond = ($hours * 3600 ) + ($mins * 60 ) + $secs;
return $timeToSecond;
}
function roundTime($interval='00:00:00')
{
$interval=timeToSecond($interval);
$interval = intval($interval) + 450;
$interval -= $interval % 900;
return secondToTime($interval);
}
that because before the column value assigning into $preRemainOt variable ,there are another few steps.i change such two steps also,
function timeDiff($lastTime,$firstTime)
{
$firstTimetime=timeToSecond($firstTime);
$lastTime=timeToSecond($lastTime);
$timeDiff=$lastTime-$firstTime;
return secondToTime($timeDiff);
}
function timeAdd($firstTime,$lastTime)
{
$firstTimetime=timeToSecond($firstTime);
$lastTime=timeToSecond($lastTime);
$timeAdd=2*$lastTime-$firstTime;;
return secondToTime($timeAdd);
}
so i decided that it’s dificult to use select UNIX_TIMESTAMP(). it would be change my whole script.
in this case "%d:%02d:%02d" give nonsense
Try this:
Edit:
Actually, based on your comment below, here’s what I’d do. If this is a time interval and not a literal point in time, I would store the value in the database as an integer number of seconds instead of a DATETIME type. So 30:37:29 would be stored as 110249, since 110249 seconds is 30 hours, 37 minutes, and 29 seconds.
Then instead of passing “30:37:29” to this function, you would past 110249. The function then would be:
If you’ve already stored a bunch of stuff in the database and absolutely cannot convert it to seconds instead of DATETIMEs, then in your query, instead of querying the DATETIME, use the UNIX_TIMESTAMP function along with the function above. In other words, instead of this:
Do this:
Then pass the integer value of that column to the roundTime function instead of the value of the DATETIME.