Bit of a puzzle for you guys. I can come up with blunt workaround (below) but I thought it would present it to you all to see if there was a more elegant solution out there. Keep in mind I'm looking for a PHP or MySql only solution.
Tables is 'candles2'. I have a MySQL TEXT column 'time' that has a datestamp in it ( e.g. 2010.08.13 19:30 ). Please don't ask why it is a TEXT field :). I have a second TEXT column 'timeframe' that lists a denomination of time (e.g. '15m', '30m', '1h', '4h' or 'daily'). 'id' is the primary key.
I want to add the amount of time in 'timeframe' to 'time'.
So for example if 'time' == '2010.08.13 19:30' and 'timeframe'==15m then it would update 'time' with '2010.08.13 19:45' (notice the addition of 15 minutes).
Here is what I was thinking:
//PHP querying the DB:
//using UNIX_TIMESTAMP to make the datetime usable by PHP
$sql = "SELECT UNIX_TIMESTAMP(time), timeframe, id FROM candles2";
$result = mysql_query($sql);
//loop through rows
while($row = mysql_fetch_array($result))
{
//call function to determine the appropriate # of seconds to add
$newTime = newTime($row['time'],$row['timeframe']);
//build UPDATE query and update row
$update_query = "UPDATE candles2 SET 'time'= FROM_UNIXTIME(" . $newTime . ") WHERE id='".$row['id']."'";
$update_result = mysql_query($update_query);
}
//PHP Function to add appropriate seconds
function newTime($oldtime,$timeframe)
{
switch ($timeframe)
{
case "15m": return($oldtime + 15 * 60);
case "30m": return($oldtime + 30 * 60);
case "1h": return($oldtime + 60 * 60);
case "4h": return($oldtime + 240 * 60);
case "daily": return($oldtime + 1440 * 60);
default: die("whoops, something broke");
}
}