5

Is it possible to use mysqls NOW() function and add seconds to or from it?

Like so;

$q = $dbc -> prepare ("UPDATE account SET time = NOW() + $seconds WHERE id = ?"); 

Thanks

0

5 Answers 5

17

If you want to do this in MySql, you can either use

DATE_ADD(NOW(), INTERVAL $seconds SECOND) 

or

UNIX_TIMESTAMP() + $seconds 

Source

Sign up to request clarification or add additional context in comments.

Comments

1

You can add seconds to it with DATE_ADD():

$q = $dbc -> prepare ("UPDATE account SET time = DATE_ADD(NOW(), INTERVAL $seconds SECONDS) WHERE id = ?"); 

1 Comment

Beware though that if I get $seconds = "little johhny tables;" into your PHP, you've a gaping hole. You probably want to use your DB-layers placeholder system (the ?) instead of simply injecting $seconds.
1
$q = $dbc -> prepare ("UPDATE account SET time = date_add(NOW() + INTERVAL $seconds SECOND) WHERE id = ?"); 

Comments

-1

No it is not possible.

You can just do:

$time = date('Y-m-d H:i:s', strtotime("+2 seconds")); $q = $dbc->prepare ("UPDATE account SET time = '$time' WHERE id = ?"); 

Comments

-1

I don't know your exact dbc class usage, but most probably, you want to simply add another parameter:

$q = $dbc->prepare ("UPDATE account SET time = NOW() + ? WHERE id = ?"); $q->execute($seconds, $id); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.