cgwebprojects
cgwebprojects

Reputation: 3472

mysql date add or date sub with variable?

Is it possible to use mysql date add or sub with a variable if I set it.

UPDATE table SET time = DATE_ADD(NOW(), INTERVAL $minutes MINUTES);

I have tried but can't get it working?

Thanks

Upvotes: 1

Views: 1973

Answers (3)

Jason McCreary
Jason McCreary

Reputation: 73011

Coming in late to advocate protection from SQL Injection.

$qry = 'UPDATE table SET time = DATE_ADD(NOW(), INTERVAL ' . (int)$minutes . ' MINUTE)';

Note: Credit to diEcho and fivedigit for their foundational answers.

Upvotes: 3

fivedigit
fivedigit

Reputation: 18682

Assuming you're using PHP to get the expression part of DATE_ADD done right, you're using MINUTES rather than MINUTE, which is the unit you're looking for:

UPDATE table SET time = DATE_ADD(NOW(), INTERVAL $minutes MINUTE);

Also see the MySQL documentation

Upvotes: 4

xkeshav
xkeshav

Reputation: 54052

TRY

$qry = "UPDATE table SET time = DATE_ADD(NOW(), INTERVAL ".$minutes." MINUTE) ";

Upvotes: 2

Related Questions