I want to convert times to rounded times.
Therefore I only want to use intervals in seconds.
It works well for quarter hours and full hours. But not for days. Then it is messed up. But I can't understand why
This is my function:
function formatToMySQLTime($sTime,$iInterval){
switch ($iInterval){
case 1: // 15 minutes;
$iDivider = 15 * 60;
break;
case 2: // 1 hour
$iDivider = 60 * 60;
break;
case 3: // 1 day
$iDivider = 60 * 60 * 24;
break;
}
$iRemainder = strtotime($sTime) % $iDivider;
$iRoundTime = strtotime($sTime) - $iRemainder;
$sTime = date('Y-m-d H:i:s', $iRoundTime);
return $sTime;
}
Here is the output:
echo formatToMySQLTime('2013-10-21 03:23:00',1);
2013-10-21 03:15:00
echo formatToMySQLTime('2013-10-21 03:23:00',2);
2013-10-21 03:00:00
echo formatToMySQLTime('2013-10-21 03:23:00',3);
2013-10-21 02:00:00
The third output is wrong, it should be 2013-10-21 00:00:00
. What is my mistake?