MySQL add time to column
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");
}
}
Why not use MySQL's STR_TO_DATE()
to transform your text value into a datetime value, then use DATE_ADD()
to add the interval? It should be fairly easy to to transform your timeframe into an interval format that MySQL expects (INTERVAL 30 MINUTE
for example). You can then use DATE_FORMAT()
to get it back into text form.
change your newtime function to the following:
function newTime($oldtime,$timeframe)
{
$new_time = strtotime( $oldtime );
switch ($timeframe)
{
case "15m":
$new_time = $new_time + 15 * 60;
break;
case "30m":
$new_time = $new_time + 30 * 60;
break;
case "1h":
$new_time = $new_time + 60 * 60;
break;
case "4h":
$new_time = $new_time + 240 * 60;
break;
case "daily":
$new_time = $new_time + 1440 * 60;
break;
}
return date( 'Y.m.d H:i', $new_time );
}
You can use the PHP DateTime class to make date and time calculations much simpler:
$timeframe = '15m';
$time = '2010.08.13 19:30';
$dt = DateTime::createFromFormat('Y.m.d G:i', $time);
$new_dt = $dt->add(
new DateInterval('PT' . strtoupper(
str_replace('daily', '24h', $timeframe))));
echo date_format($new_dt, 'Y.m.d G:i');
// 2010.08.13 19:45
Or, for a purely MySQL solution, you could use a cursor to create a prepared statement to update the rows:
DELIMITER //
DROP PROCEDURE IF EXISTS update_candles//
CREATE PROCEDURE update_candles()
BEGIN
DECLARE var_id INT;
DECLARE var_time, var_timeframe TEXT;
DECLARE var_done INT DEFAULT 0;
DECLARE csr_candles CURSOR FOR
SELECT id, time, timeframe FROM candles2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET var_done = 1;
OPEN csr_candles;
REPEAT
FETCH csr_candles INTO var_id, var_time, var_timeframe;
IF NOT var_done THEN
SET
@id := var_id,
@time := var_time,
@timeframe := var_timeframe,
@unit1 := SUBSTR(@timeframe, -1),
@unit2 := MAKE_SET(FIND_IN_SET(@unit1, 'm,h,,y'), 'MINUTE', 'HOUR', 'DAY'),
@interval := FORMAT(@timeframe, 0) | 1,
@var_sql := CONCAT('UPDATE candles2 SET time = TIMESTAMPADD(', @unit2, ',?,?) WHERE id = ?');
PREPARE sql_stmt FROM @var_sql;
EXECUTE sql_stmt USING @interval, @time, @id;
END IF;
UNTIL var_done END REPEAT;
DEALLOCATE PREPARE sql_stmt;
END;
//
DELIMITER ;
CALL update_candles;
精彩评论